2013-07-30 4 views
0

Я пытаюсь найти все элементы engagement_app, которые имеют идентификаторы и которые не имеют идентификаторов. Что не так с приведенным ниже кодом? Он не может найти тот, с/без идентификаторовjQuery find id атрибут существует или нет

jQuery('.engagement_data').each(function() { 

    var engagement_app = $(this).find(".engagement_app").val(); 
    //if ($(this).find(".engagement_app").attr('id')) 
    if ($(this).find(".engagement_app").is('[id]')) 
    { 
     console.log("in if1") 
     console.log($(this).find(".engagement_app").attr('id')); 
    } 

    if ($(this).find(".engagement_app").not('[id]')) 
    { 
     console.log("id not found") 
    } 
}); 
+0

Вы должны взглянуть на документацию, чтобы узнать, что '.not' фактически делает: HTTP: //api.jquery .com/не /. Также см .: http://stackoverflow.com/questions/31044/is-there-an-exists-function-for-jquery?rq=1. –

ответ

2

Попробуйте -

использования ! вместо not

if (! $(this).find(".engagement_app").is('[id]')){ 
    console.log("id not found") 
}else{ 
    console.log("in if1") 
    console.log($(this).find(".engagement_app").attr('id')); 
} 
0

Если вы хотите "all the elements of engagement_app" и без идентификатора следует использовать (chilldren?) что-то вроде того.

var els_with_id = $(this).find(".engagement_app *[id]") 

и

var els_without_ids = $(this).find(".engagement_app *").not("[id]") 
0

Вы можете использовать

$('.engagement_app [id]') 

Чтобы найти все элементы с ID, и

$('.engagement_app :not([id])') 

Чтобы найти все элементы без ID.

Вот скрипку, которая демонстрирует, как это может работать:

http://jsfiddle.net/qj3V7/

0
<html> 
<head> 
<script src="http://code.jquery.com/jquery-1.9.1.js"></script> 
<script> 
$(document).ready(function(){ 

$(".engagement_app").each(function(){ 
var id = $(this).attr("id"); 

if(id==undefined) 
{ 
    console.log("id not found") 
} 
else 
{ 
    console.log("in if1"); 
    console.log($(this).attr('id')); 
} 
}); 


}); 
</script> 
</head> 
<body> 
<div class="engagement_data"> 
<div class="engagement_app" id="1">1</div> 
    <div class="engagement_app">2</div> 
    <div class="engagement_app" id="3">3</div> 
    <div class="engagement_app">4</div> 
</div> 

</body> 
</html> 
Смежные вопросы