使用JQuery为DOM元素添加类型

add type to DOM element with JQuery

本文关键字:添加 类型 元素 DOM JQuery 使用      更新时间:2023-09-26

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

我一直在玩遍历的想法在实现这一点,但我不知道它是否可以工作。跟这个差不多

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

我知道这是添加一个类的元素,但它可以以类似的方式添加TYPEtr 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'/>

你不能添加type="checkbox到td,它是无用的和无效的,但你可以添加复选框到td,例如

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

可以在td中添加一个输入元素。

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

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

不能添加type = 'checkbox',否则无效。只需在td

中添加输入类型
$('#myID tr').find("td").last().append("<input type='checkbox' />");

evert last td means

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

添加type = 'checkbox'并不意味着它是错误的html。

$('#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