如何确定使用jQuery的事件时的HTML输入元素类型?
问题描述:
考虑下面的示例代码:如何确定使用jQuery的事件时的HTML输入元素类型?
$(document).ready(function(){
$(":input").blur(function(){
alert("The input type is:"); //How would this look??????
})
});
我怎样才能detemine这是否是一个输入,选择,文本等?
这不是一个真实的例子,但应该能满足这个问题
答
$(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当然这个工作,并且不浪费时间再次包装这个$ –