Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 设置嵌套li元素的样式_Javascript_Jquery_Html_Css - Fatal编程技术网

Javascript 设置嵌套li元素的样式

Javascript 设置嵌套li元素的样式,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我已经看到了关于内部li元素的造型,但我关心的是: <ul> <li> content <ul> <li> more content <li> </ul> </li> </ul> 这实际上创建了我的嵌套列表,在理论上可以永远持续下去。问题是,您要按顺序附加到li元素,而不是将注释构造为树,然后将该结构附加到li元素。试着这

我已经看到了关于内部li元素的造型,但我关心的是:

  <ul>
    <li>
      content
      <ul>
        <li> more content <li>
      </ul>
    </li>
   </ul>

这实际上创建了我的嵌套列表,在理论上可以永远持续下去。问题是,您要按顺序附加到li元素,而不是将注释构造为树,然后将该结构附加到li元素。试着这样做:

  renderComments: function(comments) {
      // Return undefined if there are no comments to process;
     // alternatively, it /may/ be appropriate to return an empty UL.
     if (!comments || !comments.length) {
         return;
     }

     // Create the container UL for the comments in this level.
     var list = $('<ul>');
     for (var i = 0; i < comments.length; i++) { // Don't use for..in for arrays
         var comment = comments[i];

         // Create an LI for each comment and add it to the container
         var item = $('<li>')
             .attr('id', "comment-" + comment.id);
         // Add the LI to the UL parent
         list.append(item);
         // Add appropriate items to LI for comment
         item.append($('<div>'));
         item.append($('<h1>').text(comment.author));
         item.append($('<p>').text(comment.comment));

         // And then do the same for each child level of comments..
         var childrenList = this.renderComments(comment.children);
         if (childrenList) {
             // ..adding children (UL) container to the current item (LI)
             item.append(childrenList);
         }
     }

     // Return container to be used by caller
     return list;
  },
var item = $('<li>').attr('id', "comment-" + comment.id);
var comment = $('<div>');
var author = $('<h1>').text(comment.author);
var commentText = $('<p>').text(comment.comment);

comment.append(author);
comment.append(commentText);
item.append(comment);
list.append(item);
这还有一个额外的好处,即通过命名正在创建的每个DOM节点,使代码的可读性稍微提高


并确保查看jQuery文档,以了解其他文档是否更适合您的用例。

我不明白您的问题是什么:SIt看起来已经这样做了:您正在向LI元素添加一个div。与您实际看到的情况相比,您预期的行为是什么?@Perpatim问题在于它确实存在:@bloodyKnuckles请参见上面的commentvar d=$;d、 附加;d、 附加“”;项目1。追加;如果这是您想要设置div及其所有内容样式的内容,则将为您提供。