javascript - JQuery selector using value, but unknown attribute -
how can use jquery search elements, have specific attribute value, regardless of attribute tag?
like:
$("[*='myvalue']") should find
<a href="target.html" target="myvalue">... <tr change="myvalue">... the first one, because of "target" attribute, second 1 "change" attribute.
is there better solution iterate on attributes?
you can use custom pseudo-selector filter on attributes.
following jquery way.
$.expr[":"].attrfilter = function(elem, index, val){ var len = $(elem.attributes).filter(function () { return this.value === val[3]; }).length; if (len > 0) { return elem; } }; $('body *:attrfilter("value")').hide(); the $.expr[":"].attrfilter, extension mechanism custom selectors. can pass parameter.
syntax :
$.expr[':'].selector = function(elem, index, match) { } elemcurrent dom elementindexindex of elemmatcharray contains information custom selector,where parameter passed lies @ 3rd index. (reference 1,reference 2)match[0]– contains full pseudo-class selector call. in example :attrfilter("value")match[1]– contains selector name only. in exampleattrfiltermatch[2]– denotes which, if any, type of quotes used in parameter - expression. i.e. single (‘) or double (“). in example empty.match[3]– gives parameters, i.e. contained in brackets. in example `value
Comments
Post a Comment