【问题标题】:jQuery appended table adds closing tag at the end of the text automatically. Why?jQuery 附加表会自动在文本末尾添加结束标记。为什么?
【发布时间】:2011-12-26 10:45:12
【问题描述】:
$('#all_locations').append("<table>");
$('#all_locations').append("<tr><th>City</th></tr>");

$.each(data, function(i, item){
    $('#all_locations').append("<tr>");
    $('#all_locations').append("<td>"+item.city+"</td>");
    $('#all_locations').append("<tr>");
}

$('#all_locations').append("</table>");

使用alert($('#all_locations').html());得到的输出

<table></table>
<tr><th>City</th></tr>
<tr></tr><td>Seattle</td>
<tr></tr><td>Chicago</td>

此代码在 ajax 调用完成时触发。任何想法为什么会这样做?

假设数据变量是有效的 JSON 对象。

【问题讨论】:

    标签: jquery html json html-table append


    【解决方案1】:

    尽管 jQuery 提供了抽象,但您操作的是 DOM 中的 元素,而不是 HTML 源代码中的标签。

    jQuery('&lt;table&gt;')jQuery(document.createElement('table')) 的简写。

    您需要将表格行附加到表格,而不是容器(同样,需要将单元格添加到行)。

    【讨论】:

    • 哇不知道……但这确实有道理,因为每当我不关闭 append() 内的标签时,它都会为我做这件事……谢谢!跨度>
    【解决方案2】:

    最好的做法是创建一个 HTML 字符串来附加并在该字符串上运行一个 .append() 调用:

    //declare our output variable
    var output = '<table><tr><th>City</th></tr>';
    
    //iterate through data
    $.each(data, function(i, item){
    
        //add to output variable
        output += '<tr><td>' + item.city + '</td></tr>';
    }
    
    //append the output to the DOM
    $('#all_locations').append(output);
    

    看到人们将项目推入数组并加入该数组以进行追加是很常见的:

    //declare our output variable (array)
    var output = ['<table><tr><th>City</th></tr>'];
    
    //iterate through data
    $.each(data, function(i, item){
    
        //add to output variable
        output.push('<tr><td>' + item.city + '</td></tr>');
    }
    
    //append the output to the DOM after joining it together into a string
    $('#all_locations').append(output.join(''));
    

    【讨论】:

    • 在某些浏览器中,在 .append 将其发送到 DOM 之前,构建一个字符串数组并将它们一次连接起来会更快。
    • 你知道哪些浏览器在这两个过程中速度更快吗?
    • 我一直试图找到通过浏览器测量它的 quirksmode 文章,但找不到。不过,IIRC 主要是较旧的浏览器有所改善。
    • 谢谢@Jasper。这就是我要做的,但我想尝试我提到的方式,但它不起作用,我不明白为什么......感谢昆汀,我现在明白了))
    【解决方案3】:

    在标签中添加id 来解决这个问题。 然后将子元素添加到 id 而不是父元素。

    $(function(){
    
        for(var lvl=0;lvl<5;lvl++)
        {
            $("#tblId tbody").append("<tr id="+lvl+"/>");                   
            for(var fchr=0;fchr<3;fchr++)
                $("#tblId tbody #"+lvl).append("<td>Val"+lvl+fchr+"</td>");
        }
    
        alert($('#tblId').html());
    
    });
    

    运行示例,请参见此处: http://jsfiddle.net/WHscf/1/

    【讨论】:

      【解决方案4】:

      不要那样做,试试这样的:

      var table = $("<table>");
      if (table){
          table.append($("<tr>").append($("<th>").text("City")));
          $.each(data, function(i, item){
              table.append($("<tr>").append($("<td>").text(item.city)));
          });
          table.appendTo($("#all_locations"));
      }
      

      这是另一种更接近您目前的做法的方式:

      $("#all_locations""#all_locations").append("<tr><th>City</th></tr>"); 
      
      $.each(data, function(i, item){  
          $('#all_locations').append("<tr>");  
          $('#all_locations').append("<td>""<tr><td>" + item.city + "</td>"td></tr>");  
          $('#all_locations').append("<tr>"});  
      } 
      
      $("#all_locations tr").wrapAll("<table></table>");  
      

      【讨论】:

      • 最后一行将环绕#all_locations 元素,而不是它的内容。为什么不先将表追加到#all_locations,然后将行追加到表中?
      猜你喜欢
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-31
      • 1970-01-01
      • 1970-01-01
      • 2012-12-04
      • 1970-01-01
      相关资源
      最近更新 更多