1

试图找出一种将 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

从我可以从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));
}); 
4

2 回答 2

2

API,不需要从 an 去接近.ononclick方法就够了。

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

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>

希望这可以帮助!

于 2020-06-03T06:10:19.950 回答
-1

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

掌心

于 2020-06-06T18:17:15.983 回答