是否可以仅通过属性值选择元素? [英] Is it possible to select element by attribute value only?
本文介绍了是否可以仅通过属性值选择元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只需要使用jquery通过属性value
查找页面中的所有元素(忽略键).
I need to find all elements in a page by attribute value
only (ignoring the key) using jquery.
有没有一种方法可以轻松地做到这一点?
Is there a way to do this easily?
当前,我只是迭代页面中的所有元素,每个属性等.
Currently, I am just iterating on all elements in the page, on every property etc..
推荐答案
您可以使用$.expr
,Element.attributes
,Array.prototype.some()
$.expr[":"].attrValue = function(el, idx, selector) {
return [].some.call(el.attributes, function(attr) {
return attr.value === selector[selector.length - 1]
})
};
// filter element having attribute with `value` set to `"abc"`
$(":attrValue(abc)").css("color", "blue");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
<div title="abc">abc</div>
<div title="def">def</div>
<div title="ghi">ghi</div>
<div title="jkl">jkl</div>
这篇关于是否可以仅通过属性值选择元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文