not working on newly created list items

问题描述 投票:-1回答:2

我这是由一个点击事件填充的收藏夹列表。尽管功能工作列表项不被换行分隔:

指南Stuff.docxTemplate 1.docxTemplate 2.docxNext

Line.docxThen More.docx

我列入<br/> .append(),但没有奏效。我已经包括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>

撷取画面

javascript jquery html append line-breaks
2个回答
2
投票

添加了注释的代码,并把它清理了一下。增加了它的元素追加一个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>

2
投票

你需要<ul><ol>使用<li>。另外,<li>元素缺少结束标记。

© www.soinside.com 2019 - 2024. All rights reserved.