Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Can you please tell me if there is any DOM API which search for an element with given attribute name and attribute value:(您能告诉我是否有DOM API搜索具有给定属性名称和属性值的元素:)

Something like:(就像是:)

doc.findElementByAttribute("myAttribute", "aValue");
  ask by michael translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
442 views
Welcome To Ask or Share your Answers For Others

1 Answer

Modern browsers support native querySelectorAll so you can do:(现代浏览器支持本机querySelectorAll因此您可以执行以下操作:)

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

https://developer.mozilla.org/en-US/docs/Web/API/Document.querySelectorAll(https://developer.mozilla.org/zh-CN/docs/Web/API/Document.querySelectorAll)

Details about browser compatibility:(有关浏览器兼容性的详细信息:)

You can use jQuery to support obsolete browsers (IE9 and older):(您可以使用jQuery支持过时的浏览器(IE9和更早版本):)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...