本文介紹了jQuery - 選擇屬性名稱(不是值)以...開頭的所有元素?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
假設我正在尋找具有屬性數據語言"的所有元素,其值以java"開頭(將匹配java"和javascript").我知道該怎么做:
Say I'm looking for all elements with an attribute 'data-language', whose value begins with 'java' (would match both 'java' and 'javascript'). I know how to do this:
$('[data-language^="java"]')
但我的問題是:如何獲取所有具有屬性的元素(不是屬性的值,而是實際的屬性名稱本身)?例如:
But my question is: how do I get all elements that have an attribute (not the value of the attribute, but the actual attribute name itself) beginning with something? For example:
- 屬性名稱以data-s"開頭的所有元素,或
- 所有具有數據屬性的元素(屬性以data-"開頭).
推薦答案
沒有捷徑,可以使用attributes
集合:
There is no shortcut, you may use the attributes
collection :
$(someselector).filter(function(){
var attrs = this.attributes;
for (var i=0; i<attrs.length; i++) {
if (attrs[i].name.indexOf("someStartOfName")==0) return true;
}
return false;
});
這篇關于jQuery - 選擇屬性名稱(不是值)以...開頭的所有元素?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!