gpt4 book ai didi

javascript - 根据属性值在 DOM 中查找元素

转载 作者:IT老高 更新时间:2023-10-28 13:14:48 25 4
gpt4 key购买 nike

您能否告诉我是否有任何 DOM API 可以搜索具有给定属性名称和属性值的元素:

类似:

doc.findElementByAttribute("myAttribute", "aValue");

最佳答案

现代浏览器支持原生 querySelectorAll,所以你可以这样做:

document.querySelectorAll('[data-foo="value"]');

https://developer.mozilla.org/en-US/docs/Web/API/Document.querySelectorAll

浏览器兼容性详情:

您可以使用 jQuery 来支持过时的浏览器(IE9 及更早版本):

$('[data-foo="value"]');

关于javascript - 根据属性值在 DOM 中查找元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2694640/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com