繁体   English   中英

<br />在新创建的列表项上不起作用

[英]<br /> not working on newly created list items

我有一个由点击事件填充的收藏夹列表。 即使该功能正常运行,列表项也不会被换行符分隔:

Stuff.docxTemplate 1.docxTemplate 2.docxNext指南

Line.docx然后更多.docx

我在.append()包含了<br/> ,但是没有用。 我之前在jQuery中加入了换行符,所以我不确定为什么它现在不起作用。 有什么想法吗?

JS代码段:

function faveFunc(evt) {
    var anchor = $($(evt.target).prev().find("a")[0]).clone();
    switch($(".populate-faves").find("a:contains(" + $(anchor).text() + ")").length)
    {
      case 0:
        $(".populate-faves").append(anchor);
        break;
      default:
        $(".populate-faves > a:contains(" + $(anchor).text() + ")").remove();
        break;
    }


  }; // ------------ faveFunc

function newList() {
    let data = $($(evt.target).prev().find("a")[0]).html()
    $(".populate-faves").html("");
    $("#km-table-id tbody tr").each(function(i, el) {
      let fave = $(el).find(".checkbox-class");
      let itemText = $(el).find(data);
      if($(fave).is(":checked")) {
        // $(".populate-faves").append("<li>" + $(itemText).html() + "<br/>");
        $(".populate-faves").append("<li> <br />");
      }
    });
   console.log(newList);
}; // ----- newList()

HTML:

...

<div id="myFave.hs-gc-header" class="faves-div">
     <p style="font-weight:bold">My Favorites:</p>

     <div class="populate-faves"></div> <!-- Where I want the list items to appear -->
</div>

...

---

更新的代码:

function newList() {
    let data = $(evt.target).prev().find("a").eq(0).html();
     let outputList = $(".populate-faves .ul-faves");

     $(".populate-faves").html("");

    $("#km-table-id tbody tr").each(function(i, el) {
      let fave = $(".checkbox-class", el);
      let itemText = $(data, el);

      if(fave.prop(":checked")) {
        outputList.append("<li>" + itemText.html() + "</li>" + "<br/>");
        // $(".populate-faves ul").append("<li> <br />");
      }
    });
    // console.log(newList);
  };

HTML:

<div id="myFave.hs-gc-header" class="faves-div">
            <p style="font-weight:bold">My Favorites:</p>

            <div class="populate-faves">
              <ul class="ul-faves"></ul>
            </div>
          </div>

屏幕截图

您需要<ul><ol>才能使用<li> 此外, <li>元素缺少结束标记。

在代码中添加了注释,并对其进行了一些清理。 添加了一个ul来将元素追加到该元素。

 function newList() { let data = $(evt.target).prev().find("a").eq(0).html(); // use eq() to avoid breaking out of the jQuery object let outputList = $(".populate-faves .list"); // lookup the output list once $(".populate-faves").html(""); $("#km-table-id tbody tr").each(function(i, el) { // you can use the $(selector, context) version to avoid creating unnecessary jQuery objects let fave = $(".checkbox-class", el); let itemText = $(data, el); // if you already have the element, use prop('checked') instead of is(':checked') to access the boolean property on the element if(fave.prop("checked")) { //append the li to the list, properly closing it outputList.append("<li>" + itemText.html() + "</li>"); } }); }; 
 <div id="myFave.hs-gc-header" class="faves-div"> <p style="font-weight:bold">My Favorites:</p> <div class="populate-faves"> <ul class="list"></ul> <!-- CREATED unorder list FOR OUTPUT --> </div> </div> 

暂无
暂无

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

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