简体   繁体   English

关于使用jQuery向tr添加多个td感到困惑

[英]Confused about adding multiple td's to tr using jQuery

So I have the following code: 所以我有以下代码:

<script type="text/javascript">
    $(document).ready(
        function () {
            $("#txt").click(function () {
                var $TableRow = $('<tr></tr>');
                var $TableData = $('<td></td>');
                var $TableData2 = $('<td></td>');

                // Works
                $("#tblControls").append(
                $TableRow.html(
                $TableData.text("Test, Hello World3")
            )
    );

</script>
<div style="display: inline">
    <input type="button" id="txt" value="Add TextBox" style="" />
</div>
<br/>
<table id="tblControls" width="100%">

</table>

But why does this not add two td's to the tr? 但是,为什么不将两个td加到tr上呢?

$("#tblControls").append(
    $TableRow.html(
        $TableData.text("Test, Hello World3")
        +
        $TableData2.text("Test, Hello World4")
    )
);

What I get is this: 我得到的是:

[object Object][object Object]

Because the + operator will result in toString being called on the arguments on either side of it, which yields [object Object] (since $TableData.text("...") and such returns the jQuery object; see text for details). 因为+运算符将导致toString在其任一侧的参数上被调用,从而产生[object Object] (因为$TableData.text("...") ,这样返回jQuery对象;有关详细信息,请参见text ) 。

What you want is: 您想要的是:

$TableRow.append($TableData).append($TableData2);

See append in the documentation. append的文件中。

Once the page has been loaded, you need to think much more in terms of objects like elements and such, and less in terms of markup . 加载页面后,您需要在诸如元素之类的对象方面进行更多思考,而在标记方面进行更多思考。 When you get in the habit of that, it's very powerful. 当您养成习惯时,它非常强大。

You are trying to combine two jQuery objects, not two HTML-strings. 您正在尝试组合两个jQuery对象,而不是两个HTML字符串。 The .text() method return the jQuery object to support chaining, not the element's HTML as a string. .text()方法返回jQuery对象以支持链接,而不是元素的HTML作为字符串。

You can refer to this SO question on how to get the HTML of the entire element, including the actual element. 您可以参考此SO问题 ,了解如何获取整个元素(包括实际元素)的HTML。

This is untested, but something like this should do it: 这未经测试,但是应该这样做:

$("#tblControls").append(
        $TableRow.html(
            $("<div />").append($TableData.text("Test, Hello World3")).html() + 
            $("<div />").append($TableData2.text("Test, Hello World4")).html() 
        ) 
);

However, the jQuery documentation says this about what .append() expect: 但是,jQuery文档说明了有关.append()的期望:

DOM element, HTML string, or jQuery object to insert at the end of each element in the set of matched elements. 将DOM元素,HTML字符串或jQuery对象插入匹配的元素集中每个元素的末尾。

Since it can take a jQuery object, you can simply pass $TableData and $TableData2 to the append-method. 由于它可以使用jQuery对象,因此可以简单地将$TableData$TableData2传递给追加方法。

$("#tblControls").append(
     $TableRow.append($TableData).append($TableData2);
);

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

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