【问题标题】:List.js addEventListener and .on() not workingList.js addEventListener 和 .on() 不起作用
【发布时间】:2020-09-21 17:45:55
【问题描述】:

试图找出一种将 addEventListener 与 List.js 一起使用的方法

  • 我有一个包含一些文本和按钮的页面
  • 然后我正在加载 List.js 文件,该文件对这些元素进行分页并允许进行排序。
  • 当主脚本文件加载时,我正在为所有这些按钮添加一个事件监听器。

JS加载顺序:

1 - List.js
2 - List.js 脚本文件(具有列表 js 功能,如创建列表等)
3 - 主脚本文件(带有主 JS 代码,包括 addEventListener 代码)

如果我先加载主脚本文件,则不会创建事件侦听器(我相信它会在 List.js 加载并创建不同的分页页面时刷新。

按照我上面的方式对文件进行排序,事件监听器只会被创建并且可以用于列表的第一页,如果我切换到第 2,3 页,......他们没有得到事件监听器为按钮创建。我看到其他页面的元素实际上是动态创建的,所以这就是 addEventListener 不起作用的原因。

我目前的解决方案是在所有按钮上使用onclick="myfunction(event)",这样当 Listjs 生成列表项时,它具有内联附加的函数,但虽然这工作正常,但我觉得它有点 hacky。


我看到 List.js 有一个名为 .on() 的方法,但我似乎也无法让它工作。

我的代码:

var menuList = new List('menu-items', {
    valueNames: ['menu-item'],
    page: 3,
    pagination: true
  });

function log_console(){
    return console.log('list updated!');    
} //tried this, doesn't work

//menuList.on('updated', log_console);

menuList.on('updated', function(menuList){
    return console.log('list updated');
}); //this doesn't work either

根据我从the docs 获得的信息,每次更新列表时都应该触发它,这似乎是我从列表的第 1 页切换到第 2 页时发生的情况(它替换了 HTML)。

我还尝试了一个不同的事件,我认为它在我使用搜索框时应该可以工作,但没有任何反应:

menuList.on('searchStart', function(menuList){
  return console.log('list updated');
}); //doesn't log anything enither

我该如何使用这个 ListJs .on() 方法?每次用户切换页面并生成列表时,我都会使用它来创建事件侦听器,如下所示:

menuList.on('updated', function(menuList){           
  const btns = document.querySelectorAll('.add-btns');
  btns.forEach(button => button.addEventListener('click', myfunction));
}); 

【问题讨论】:

    标签: javascript listjs


    【解决方案1】:

    API,不需要从.on接近它,onclick方法就够了。

    这里有一个解决方案。单击运行代码段(完整页面更好)并享受一个工作示例!

    const options = {
      pagination: true,
      page: 1,
      plugins: [
         ListPagination({})
     ],
      valueNames: [ 'name', 'city' ],
      item: '<li><h3 class="name"></h3><p class="city"></p><button onclick="handleClick(this)">Log and Delete</button></li>'
    };
    const values = [
      { name: 'Lucas', city:'Stockholm' }
      ,{ name: 'Jonas', city:'Berlin' }
    ];
    const hackerList = new List('hacker-list', options, values);
    
    function handleClick(node) {
      const list = node.parentNode;
      console.log(list);
      console.log(hackerList);
      //Delete example
      const name = list.querySelector(".name").innerHTML
      console.log(name);
      hackerList.remove("name", name);
    }
    
    hackerList.on('updated', console.log);
    hackerList.add({ name: "Jonny", city: "Stockholm" });
    h2 {
      font-family:sans-serif;
    }
    .list {
      font-family:sans-serif;
      margin:0;
      padding:20px 0 0;
    }
    .list > li {
      display:block;
      background-color: #eee;
      padding:10px;
      box-shadow: inset 0 1px 0 #fff;
    }
    .avatar {
      max-width: 150px;
    }
    img {
      max-width: 100%;
    }
    h3 {
      font-size: 16px;
      margin:0 0 0.3rem;
      font-weight: normal;
      font-weight:bold;
    }
    p {
      margin:0;
    }
    
    input {
      border:solid 1px #ccc;
      border-radius: 5px;
      padding:7px 14px;
      margin-bottom:10px
    }
    input:focus {
      outline:none;
      border-color:#aaa;
    }
    .sort {
      padding:8px 30px;
      border-radius: 6px;
      border:none;
      display:inline-block;
      color:#fff;
      text-decoration: none;
      background-color: #28a8e0;
      height:30px;
    }
    .sort:hover {
      text-decoration: none;
      background-color:#1b8aba;
    }
    .sort:focus {
      outline:none;
    }
    .sort:after {
      width: 0;
      height: 0;
      border-left: 5px solid transparent;
      border-right: 5px solid transparent;
      border-bottom: 5px solid transparent;
      content:"";
      position: relative;
      top:-10px;
      right:-5px;
    }
    .sort.asc:after {
      width: 0;
      height: 0;
      border-left: 5px solid transparent;
      border-right: 5px solid transparent;
      border-top: 5px solid #fff;
      content:"";
      position: relative;
      top:13px;
      right:-5px;
    }
    .sort.desc:after {
      width: 0;
      height: 0;
      border-left: 5px solid transparent;
      border-right: 5px solid transparent;
      border-bottom: 5px solid #fff;
      content:"";
      position: relative;
      top:-10px;
      right:-5px;
    }
    <script src="//cdnjs.cloudflare.com/ajax/libs/list.js/1.5.0/list.min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/list.pagination.js/0.1.1/list.pagination.min.js"></script>
    <div id="hacker-list">
    <input class="search" placeholder="Search" />
      <button class="sort" data-sort="name">
        Sort by name
      </button>
      <ul class="list">
      </ul>
      <ul class="pagination"></ul>
    </div>

    希望这会有所帮助!

    【讨论】:

    • 我不明白为什么我的不起作用...您的示例中也没有使用分页,因此它们不是 1:1 示例
    • @UriahsVictor Pagination 需要该插件。见listjs.com/docs/plugins/pagination我已经为你更新了变化
    • 你不再需要这个插件了。它被捆绑到列表 js 中,这是我正在进行的列表示例:codepen.io/javve/pen/bBfgD 我没有在 list.js 中填充列表。它已经在 DOM 中,Listjs 只是在作用于它。但是当我在页面之间切换时 .on() 方法不起作用
    【解决方案2】:

    这很愚蠢...但是我的代码完全正确...我只是包含了错误的 JS 文件...因为我制作了一份包含新更改的副本,而正在加载的文件没有.

    捂脸

    【讨论】:

      猜你喜欢
      • 2017-09-11
      • 1970-01-01
      • 1970-01-01
      • 2013-12-29
      • 2021-04-19
      • 2013-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多