简体   繁体   English

我可以使用 Jquery 在动态表中插入结束标签和开始标签吗?

[英]Can I use Jquery to insert a closing </tr> tag and an opening <tr> tag inside a dynamic table?

I'm trying to use the code below to dynamically add closing tag followed by opening so that i creates a new row every three cells.我正在尝试使用下面的代码动态添加结束标记,然后打开,以便我每三个单元格创建一个新行。 Almost working, DOM inspector shows a TR node, problem is, something happens tr isn't closing the tr tag.几乎可以工作,DOM 检查器显示一个 TR 节点,问题是,发生了一些事情 tr 没有关闭 tr 标签。 I'm new to Jquery, is there anything I'm doing wrong with this code?我是 Jquery 的新手,这段代码有什么问题吗?

         <script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.5.1.min.js" type="text/javascript"></script>
        <script type="text/javascript">
        $(document).ready(function(){
        $('td:nth-child(3n)').after('</tr><tr>');
        });
        </script>

        <table id="mytable" width="266" border="1" cellspacing="10" cellpadding="10">
        <tbody>
          <tr>
        <?php
        function somelongassfunction(){
            return 'Hello';
            }
        function have_products($a){
            return $a<=20;
            }
        $x=0;
        while (have_products($x)) {
            echo '<td>' . somelongassfunction() . '</td>';
            $x++;
        //------------------------------------- 
        /*if (fmod($x,3) == 0) {
                        echo '</tr><tr>';
                        continue;
                        }*/
        //--------------------------------------                    
        if ($x==20){
            break;
            }   
        }   
        ?>
        </tr>
        </tbody>
        </table>    

You can't work on a DOM selection as if it was an HTML document.您不能像处理 HTML 文档一样处理 DOM 选择。 A DOM document is a hierarchy of nodes, not of tags. DOM 文档是节点的层次结构,而不是标签。 The tags in your HTML are parsed into a DOM document by the browser. HTML 中的标签被浏览器解析为 DOM 文档。 You can't then add a single bit of HTML and then expect it to be parsed back into a DOM structure.然后,您不能添加 HTML 的单个位,然后期望将其解析回 DOM 结构。

Instead, you'll need to do the wrapping in jQuery.相反,您需要在 jQuery 中进行包装。 This is a viable approach -- it may not be the most efficient.这是一种可行的方法——它可能不是最有效的。

$('td').each(function(idx) {
    if (idx % 3) {
        return;
    } else {
        $(this).nextAll(':lt(2)').andSelf().wrapAll('<tr/>');
    }
}).parent().unwrap();

jsFiddle jsFiddle

Another approach is to detach all the td elements, and then split them into groups of 3 using splice :另一种方法是detach所有td元素,然后使用splice将它们分成 3 个组:

var td = $('tr td').detach();
$('tr').remove();
while(td.length>0){ 
 $(td.splice(0,3)).wrapAll('<tr />').parent().appendTo('tbody');   
}

example: http://jsfiddle.net/niklasvh/C6unV/例如: http://jsfiddle.net/niklasvh/C6unV/

As lonesomeday said, you can't treat a DOM like a bit of HTML.正如 lonesomeday 所说,您不能将 DOM 视为 HTML。 Besides, TR closing tags are optional so when you insert some markup with an opening TR, the browser will close it.此外, TR关闭标签是可选的,因此当您插入一些带有打开 TR 的标记时,浏览器将关闭它。 You don't need a closing tag.您不需要结束标签。

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

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