【问题标题】:Add a Like + Sort Function to existing dynamic funcion向现有动态函数添加 Like + Sort 函数
【发布时间】:2020-06-12 13:10:50
【问题描述】:

我写了一个函数,它基于一个 json 数据库为我动态创建一个网页。

现在我想添加 2 个函数:

  1. 如果您点击点赞 img(它有 id 按钮),则网页上的点赞计数器应该会增加 1。非常简单,只需使用 jQuery 变量 ++ 点按(点击)然后 .text(variable)

  2. 一个排序功能 - 根据收到的喜欢一个项目,您应该能够对其进行排序(最喜欢的 div 第一,第二,第三......

当我给所有类似的按钮并输出一个单独的 id 时,我可以为每个单独的变量单独编写它,但我想让它动态化,所以如果你将新数据添加到 json 文件,它会动态地与 like 和 sort 函数一起工作.

点赞暂时没有保存在任何地方。

自从坐在上面 3 小时和谷歌这么多堆栈溢出后,我想我用不同的东西使我的大脑超负荷,现在似乎没有任何工作^^

function filmInsert(insert) {
    $.each(film, function(i, data) { //.each statt loop
      let box = 

      `<div class="boxwrapper">
      <div class="imgbox">
      <img src="${data.img}" alt="${data.titel}">
    </div>
    <div class="textbox">
        <h3>${data.titel}</h3>
        <p>${data.beschreibung}</p>
        <p> <a id="button${data.id}">
          <img src="img/budspencer_official.png"> Like
          </a>
          <span class="output${data.id}">${data.likes}</span>
        </p>
      </div>
    </div>`;
      insert.append(box);
    });
  }

【问题讨论】:

    标签: javascript html jquery


    【解决方案1】:

    我已经为 boxwrapper 项目添加了一个容器元素,因为我假设你有一个,而且最好有一个,而不是仅仅将已排序的项目添加到 HTML 文档的正文中。

    $(document).on("click", ".textbox a", function() {
      let likes = parseInt($(this).closest(".textbox").find("span").text());
      $(this).closest(".textbox").find("span").text(likes + 1);
    });
    $("#sort").on("click", function() {
      let divs = $(".boxwrapper")
      let sorted = divs.sort(function(a, b) {
         return $(a).find("span").text() < $(b).find("span").text() ? 1 : -1;
      });
      $(".container").html(sorted);
    
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="container">
      <div class="boxwrapper">
        <div class="imgbox">
          <img src="example.gif" alt="Title">
        </div>
        <div class="textbox">
          <h3>Titel</h3>
          <p>Description</p>
          <p> <a id="button1">
              <img src="https://dummyimage.com/40x40/000/fff&text=1"> Like
            </a>
            <span class="output1">0</span>
          </p>
        </div>
      </div>
      <div class="boxwrapper">
        <div class="imgbox">
          <img src="example.gif" alt="Title">
        </div>
        <div class="textbox">
          <h3>Titel 2</h3>
          <p>Description 2</p>
          <p> <a id="button2">
              <img src="https://dummyimage.com/40x40/000/fff&text=2"> Like
            </a>
            <span class="output2">0</span>
          </p>
        </div>
      </div>
    </div>
    
    <button id="sort">
      Sort
    </button>

    【讨论】:

    • @TomersonJefferson 这很奇怪,因为它在示例中起作用。您的排序按钮是动态添加的吗?能否在排序函数中添加控制台消息,看是否被调用?
    • @TomersonJefferson 我得再问一遍:您的排序按钮是通过代码添加的还是最初在页面中?并且可以在排序函数中添加一个控制台消息来检查它是否被调用?
    • @TomersonJefferson 感谢您的澄清。你能用你生成的 HTML 做一个 Fiddle 以便检查问题吗?
    • @TomersonJefferson 这很奇怪。我已经测试了 test.html 并且排序在 Firefox 中有效,但在 Chrome 和 IE 中无效。我去看看。
    • @TomersonJefferson 我刚刚找到了一个修复程序,并在我的答案中更新了排序函数的代码。它现在可以在 Firefox、Chrome 和 IE 中运行,并且应该也可以在其他浏览器中运行。
    猜你喜欢
    • 2016-06-15
    • 2019-05-17
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 2016-02-10
    • 1970-01-01
    • 2011-02-16
    • 2021-05-14
    相关资源
    最近更新 更多