更新时间:2022-06-13 00:04:12
通过查看是否支持querySelectorAll,可以使用适用于新旧浏览器的版本
Heres a version that will work in old and new browsers by seeing if querySelectorAll is supported
您可以通过调用 getElementsByAttribute(属性)来使用它,价值)
这是一个小提琴: http://jsfiddle.net/ghRqV/
var getElementsByAttribute = function(attr, value) {
if ('querySelectorAll' in document) {
return document.querySelectorAll( "["+attr+"="+value+"]" )
} else {
var els = document.getElementsByTagName("*"),
result = []
for (var i=0, _len=els.length; i < _len; i++) {
var el = els[i]
if (el.hasAttribute(attr)) {
if (el.getAttribute(attr) === value) result.push(el)
}
}
return result
}
}