有沒有辦法在jQuery來獲得多個屬性jQuery的獲取多個屬性
<input type="text" title="hello there" class="maiz"/>
(function() {
var inputTitle = $("input").attr("title","class");
console.log(inputTitle[1])//output:undefined
})();
我是新來的jQuery
有沒有辦法在jQuery來獲得多個屬性jQuery的獲取多個屬性
<input type="text" title="hello there" class="maiz"/>
(function() {
var inputTitle = $("input").attr("title","class");
console.log(inputTitle[1])//output:undefined
})();
我是新來的jQuery
您可以使用元素對象的屬性屬性得到屬性:
var el = document.getElementById("someId");
var attributes = el.attributes; // Here they are
在jQuery中你可以用get()方法的原始元素,如:
var el = $("input").get(0);
var attributes = el.attributes; // Here they are
你不能得到多個屬性,你只需要再次調用attr
:
var input = $("input");
var title = input.attr("title");
var cls = input.attr("class");
你的示例設置值「類」的稱號屬性。
或更多類似到您的原代碼:
var inputTitle = [input.attr("title"), input.attr("class")];
inputTitle[1]; // gives you 'maiz'
試試這個:
$('.maiz').click(function(){
for(var i = 0;i < this.attributes.length;i++){
console.log(this.attributes[i]);
}
});
你可以試試這個:
for (var i = 0; i < elem.attributes.length; i++) {
var attrib = elem.attributes[i];
if (attrib.specified == true) {
console.log(attrib.name + " = " + attrib.value);
}
}
不建議使用'使用屬性'指定的屬性。它總是返回true.' - 根據Firefox – Dennis
jQuery選擇工作得很像CSS選擇器。 $('selector')
如果你想選擇所有元素,如果一個特定的類 $('.class')
如果你想選擇類和編號 $('.class, #id')
這基本上是它。除非你有一個具體的問題
檢查了這一點。 http://stackoverflow.com/questions/2048720/get-all-attributes-from-a-html-element-with-javascript-jquery –
是不是可以通過attr()方法? –