除非data-*自定义数据属性的值是固定不变的,否则最好不要把data-*作为查询条件。

例子:

<div data-index="0">hello</div>

var div= document.querySelector("div[data-index=\"0\"]");

console.log(div);//正常

div.dataset.index=1;//更新属性值

var newDiv= document.querySelector("div[data-index=\"1\"]");//使用更新后的属性值来查询元素

console.log(newDiv);//查询不正常,找不到data-index为1的元素,因为自定义数据的值被修改后不会同步到页面标签属性中,此时data-index属性值仍是0,更新的不过是dataset中的值

相关文章:

  • 2021-10-10
  • 2022-12-23
  • 2021-12-18
  • 2021-05-17
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-01-11
  • 2021-07-30
  • 2021-12-01
  • 2022-01-27
相关资源
相似解决方案