[英]Check if Attribute('id') Exists in jQuery
How can I check if an attribute Id exists in jQuery? 如何检查jQuery中是否存在属性Id?
I searched around and found that this should work: 我四处搜索,发现这应该有效:
if ($(this).attr('id').attr('id'))
{
}
I still get this error: TypeError: Object gauge1 has no method 'attr' 我仍然得到这个错误:TypeError:对象gauge1没有方法'attr'
This itself will work: 这本身将起作用:
if($(this).attr("id"))
Check existing jsfiddle on the issue: http://jsfiddle.net/rwaldron/wVqvr/4/ 在问题上检查现有的jsfiddle: http : //jsfiddle.net/rwaldron/wVqvr/4/
Just try it the old way 试试老方法吧
if (this.id) {
//do something
}
You can use the is
method and the Has Attribute Selector : 您可以使用is
方法和Has属性选择器 :
if ($(this).is('[id]')) {
}
if ($(this).attr('id')){ alert(id); }
you could also just use the plain js object 你也可以使用普通的js对象
if( this.id !== undefined && this.id.length > 0 ){
//todo: use id
}
HTML: HTML:
<div class="hey"></div>
<div class="you" id="woah"></div>
<div id="results"></div>
jQuery: jQuery的:
var id = $('div.hey').attr('id');
if (id) {
// Have an id.
} else {
// Don't have an id.
}
A fiddle: http://jsfiddle.net/NEVYT/ 小提琴: http : //jsfiddle.net/NEVYT/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.