属性IDがjQueryに存在するかどうかを確認するにはどうすればよいですか?
私は周りを検索し、これが機能するはずであることがわかりました:
if ($(this).attr('id').attr('id'))
{
}
私はまだこのエラーが発生します: TypeError: Object Gauge1 has no method 'attr'
属性IDがjQueryに存在するかどうかを確認するにはどうすればよいですか?
私は周りを検索し、これが機能するはずであることがわかりました:
if ($(this).attr('id').attr('id'))
{
}
私はまだこのエラーが発生します: TypeError: Object Gauge1 has no method 'attr'
これ自体は機能します:
if($(this).attr("id"))
問題に関する既存の jsfiddle を確認してください: http://jsfiddle.net/rwaldron/wVqvr/4/
昔のやり方でやってみる
if (this.id) {
//do something
}
is
メソッドとHas Attribute Selectorを使用できます。
if ($(this).is('[id]')) {
}
if ($(this).attr('id')){
alert(id);
}
HTML:
<div class="hey"></div>
<div class="you" id="woah"></div>
<div id="results"></div>
jQuery:
var id = $('div.hey').attr('id');
if (id) {
// Have an id.
} else {
// Don't have an id.
}
フィドル: http: //jsfiddle.net/NEVYT/
プレーンな js オブジェクトを使用することもできます
if( this.id !== undefined && this.id.length > 0 ){
//todo: use id
}