2013-06-05 84 views
0

如何檢查具有特定屬性值的元素是否存在於div標籤內。如何檢查具有特定屬性值的html元素是否存在

例如,

<div id='testdiv'> 
    <a dir='hello' href='#' ></a> 
</div> 
<input type='button' id='btn' /> 

這裏是我的jQuery的。

$('#btn').click(function(){ 
    if(/*Here I need the condition to check whether, `a` tag with attribute `dir` with value `hello` is present inside `div` */) 
    { 
     alert(present); 
    } 
} 

請教親愛的朋友,因爲我是jQuery的初學者。謝謝。

+0

合併解決這些問題:[查找與jquery的特定屬性值的所有元素(http://stackoverflow.com/q/4958081)和[jQuery是否有「存在」函數?](http://stackoverflow.com/q/31044)。 –

回答

0
$('#btn').on('click', function(){ 
    if ($('#testdiv a[dir="hello"]').length) { 
     alert(present); 
    } 
}); 
0
$('#btn').click(function(){ 
    if($("div").find("a[dir=hello]").length == 1) 
    { 
     alert(present); 
    } 
} 
0
$('#btn').on('click', function(){ 
     if ($('#testdiv a').attr('dir') == 'hello') { 
      alert('1111111'); 
     } 
     else 
      alert('222'); 
    }); 

Also see this fiddle

+0

看看http://stackoverflow.com/editing-help瞭解如何正確格式化代碼。 –

相關問題