简体   繁体   English

jQuery使用附录将div包装我的内容

[英]Jquery wrapping my content with divs using append

  for(var i=0; i<num_cols; i++)
  {

    //Wrapper for column
    $('#cupcake-list').append('<div>');
    //end wrapper

    col_count++;
    num_in_col = rowsInCol(total,num_perCol,col_count);
    start = i*num_perCol;
    end = start + num_in_col;
    for(var d=start; d<end; d++)
    {
      $('#cupcake-list').append('<p>'+cupcakeData[d].name+'</p>');
    }


      //Wrapper for column
    $('#cupcake-list').append('</div>');
    //end wrapper


  }

I just want to encapsulate my p tags within div tags to act as rows, however all I get are <div></div><p>ssdfsdf</p><p>sdfsdfdsf</p><div></div>etc.... 我只想将我的p标签封装在div标签中作为行,但是我得到的只是<div></div><p>ssdfsdf</p><p>sdfsdfdsf</p><div></div>etc....

What's the best way of doing it? 最好的方法是什么?

Start with a fragment so that you don't access the DOM more than once, and append it all at the end. 从一个片段开始,这样您就不会多次访问DOM,并在最后附加所有内容。 You can skip the wrap by starting with your empty fragment, like so: 您可以从空片段开始跳过换行,如下所示:

var $fragment;
for(var i=0; i<num_cols; i++)
  {
    $fragment = $('<div />');
    col_count++;
    num_in_col = rowsInCol(total,num_perCol,col_count);
    start = i*num_perCol;
    end = start + num_in_col;
    for(var d=start; d<end; d++)
    {
      $fragment.append('<p>'+cupcakeData[d].name+'</p>');
    }


      //Wrapper for column
    $('#cupcake-list').append($fragment);
    //end wrapper


  }

This is a much faster way to do it! 这是一种更快的方法! Append parts of a string to an array and then you only have to update the DOM once. 将字符串的一部分附加到数组,然后只需更新一次DOM。

var a = [];
for(var i=0; i<num_cols; i++)
{
    a.push('<div>');
    col_count++;
    num_in_col = rowsInCol(total,num_perCol,col_count);
    start = i*num_perCol;
    end = start + num_in_col;
    for(var d=start; d<end; d++)
    {
        a.push('<p>'+cupcakeData[d].name+'</p>');
    }

    a.push('</div>');
}
$('#cupcake-list').append(a.join(''));

EDIT: I'll explain why yours wasn't working. 编辑:我会解释为什么你的不工作。 When you were calling $('#cupcake-list').append('<div>'); 当您调用$('#cupcake-list').append('<div>'); you thought it would only add the opening div tag, but that is not the case. 您以为只会添加开头的div标签,但事实并非如此。 jQuery won't let you do this is because they want to make sure the html is valid after every function call. jQuery不允许您这样做是因为他们想确保在每次函数调用之后html都是有效的。 If you were to just add the opening div and then do some other stuff, the next closing div ( </div> ) in the document would close the div you just opened, changing the structure of the document entirely. 如果只添加开始div,然后执行其他操作,则文档中的下一个关闭div( </div> )将关闭您刚打开的div,从而完全改变文档的结构。

In summation: $('#cupcake-list').append('<div>'); 总而言之: $('#cupcake-list').append('<div>'); and $('#cupcake-list').append('</div>'); $('#cupcake-list').append('</div>'); will both append <div></div> to the document. 都将<div></div>附加到文档中。 Also, access and update the DOM as if it costs you a million dollars because it is among the slowest things you can do in javascript. 另外,访问和更新DOM就像花费一百万美元一样,因为它是您可以用javascript做的最慢的事情之一。

jQuery有一个称为.wrap的方法,以及一些类似的方法( .wrapAll )。

If you are having the output that you showed, your code is not reaching the inner for, so you have a logic problem. 如果您具有显示的输出,则您的代码未达到内部要求,因此存在逻辑问题。 I think your way of doing this is correct. 我认为您这样做的方法是正确的。 When i need to build some structure on the fly i usually do the same thing. 当我需要即时建立一些结构时,我通常会做同样的事情。

JQuery append adds DOM nodes, not HTML. jQuery append添加DOM节点,而不是HTML。 So you can accomplish your task like this: 因此,您可以像这样完成您的任务:

for(var i=0; i<num_cols; i++)
{
  col_count++;
  num_in_col = rowsInCol(total,num_perCol,col_count);
  start = i*num_perCol;
  end = start + num_in_col;
  for(var d=start; d<end; d++)
  {
    $('#cupcake-list').append($('<div></div>').append('<p>'+cupcakeData[d].name+'</p>'));
  }
}

First, $('<div></div>') creates a new empty div element not yet attached to the page (you can also do $('<div>') as a shorthand if you want). 首先, $('<div></div>')创建一个尚未连接到页面的新的空div元素(如果需要,您也可以将$('<div>')做为简写形式)。 Then .append('<p>...</p>') adds a p element inside the div. 然后.append('<p>...</p>')在div内添加一个p元素。 Finally, $('#cupcake-list').append(...) adds the whole div to the end of #cupcake-list . 最后, $('#cupcake-list').append(...)将整个div添加到#cupcake-list的末尾。

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

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