javascript – 如何使用jquery查詢包含與字首匹配的data- *屬性的元素
我想建立一個選擇器來查詢以字串開始的屬性的元素.在這一點上,我假設這個選擇器不存在.
我需要擴充套件選擇器功能嗎?ofollow,noindex" target="_blank">Extending jQuery’s selector capabilitiesby James Padolsey
我需要表達類似於屬性字首選擇器[name | =“value”]的內容,但是不需要匹配“value”,我需要匹配屬性的名稱,而不是屬性的值.
<tag data-plugin-option1 =“val1”data-plugin-option2 =“val2”/>
我想最終得到一個這樣的語法:
$(‘tag(:attr | =“data-plugin”)’),它應該找到元素標籤,因為它至少有一個以資料外掛開頭的元素
嗯,我想我正在讀你的問題.
我閱讀它的方式,你想建立一個自定義選擇器,選擇具有給定屬性名稱的元素(或該名稱的開頭).
如果是這樣,我想你需要迭代每個元素的屬性集合.
$.extend($.expr[':'],{ attrNameStart: function(el,i,props) { var hasAttribute = false; $.each( el.attributes, function(i,attr) { if( attr.name.indexOf( props[3] ) !== -1 ) { hasAttribute = true; return false;// to halt the iteration } }); return hasAttribute; } });
$('img:attrNameStart(data-plugin)')
http://stackoverflow.com/questions/7602410/how-do-i-find-elements-that-contain-a-data-attribute-matching-a-prefix-using-j