繁体   English   中英

使用JQuery向DOM元素添加类型[重复]

[英]add type to DOM element with JQuery [duplicate]

这个问题已经在这里有了答案:

我正在尝试将type=checkbox附加到每个tr的最后一个td

我一直在遍历实现此目标的想法,但是我不知道它是否可以工作。 与此类似。

$('#myID tr').children("td:nth-last-of-type(1)").addClass("me");

我知道这是在元素中添加类,但是可以通过类似的方式将TYPE添加到tr DOM元素中吗? 有一个或两个例子,有像一个,但它的周围改变的类型,并且不添加一个新的。

<table id="myID" class="myClass">
    <tr>
        <th>one</th>
        <th>two</th>
        <th>three</th>
        <th>four</th>
    </tr>
    <tr>
        <td>1</td>
        <td>2</td>
        <td>3</td>
        <td>4</td>
    </tr>
    <tr>
        <td>a</td>
        <td>b</td>
        <td>c</td>
        <td type='checkbox'></td>    <!--  **THIS IS WAHT IM AFTER**  -->
    </tr> 
</table>

<input type='button' id='but_id' value='Click Me'/>

您可以将输入元素附加到td。

$(document).ready(function () {
        $('#myID tr').children("td:nth-last-of-type(1)").append("<input type='checkbox'/>");
    });

演示: http : //plnkr.co/edit/qVIUDfAxp2vUIxw2AQEp?p=preview

您不能将type="checkbox添加到td,它是无用且无效的,但是您可以将checkbox添加到td例如

$('#myID tr').children("td:nth-last-of-type(1)").append('<input type="checkbox" />');

您不能在td中添加type = 'checkbox' ,这是无效的。 只需将输入类型附加到td中

$('#myID tr').find("td").last().append("<input type='checkbox' />");

上一次td意味着

 $('#myID tr').find("td:nth-last-child(1)").append("<input type='checkbox' />");

演示

添加type = 'checkbox'并不意味着它的HTML错误。 您可以在td中添加复选框。

$('#myID tr').children("td:last-child").append('<input type="checkbox" />');

演示

您是不是要在最后一个单元格后附加一个复选框? 尝试这个。

$("#myID tr td:last-child").append("<input type='checkbox' />");

http://jsbin.com/zerocasu/2/edit

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM