2013-06-05 82 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了解如何正确格式化代码。 –

相关问题