考慮下面的示例代碼:如何確定使用jQuery的事件時的HTML輸入元素類型?
$(document).ready(function(){
$(":input").blur(function(){
alert("The input type is:"); //How would this look??????
})
});
我怎樣才能detemine這是否是一個輸入,選擇,文本等?
這不是一個真實的例子,但應該能滿足這個問題
考慮下面的示例代碼:如何確定使用jQuery的事件時的HTML輸入元素類型?
$(document).ready(function(){
$(":input").blur(function(){
alert("The input type is:"); //How would this look??????
})
});
我怎樣才能detemine這是否是一個輸入,選擇,文本等?
這不是一個真實的例子,但應該能滿足這個問題
$(this).attr("type");
見jQuery的Selectors/Attribute文檔的附加信息的目的。
$(this).attr("type");
例如:
$(document).ready(function(){
$("input").blur(function(){
alert("The input type is:" + $(this).attr("type"));
})
});
這應該工作...
$(document).ready(function(){
$("input").blur(function(){
var type = this.type;
alert("The input type is:" + type);
})
});
爲什麼不經歷,看看有什麼屬性/屬性將是最有用的?
$(document).ready(function(){
$("input").blur(function(){
for (var x in this)
alert(x + ":" + this[x]);
})
});
我怎樣才能deteminedetermine這是否是一個輸入,選擇,文本等?
注意select
,textarea
, 「等」 的元素並不受$('input')
。你可能寧願使用$(':input')
來獲得全部。
$(document).ready(function(){
$(':input').blur(function(){
alert('The tag is:' + this.tagName);
if (this.tagName == 'INPUT') {
alert("The input type is:" + $(this).attr('type'));
}
})
});
爲什麼downvote? – Ryan
+1當然這個工作,並且不浪費時間再次包裝這個$ –