tec*_*ant 2 html anchor jquery if-statement
遇到一个简单的问题.我的早茶不够结实.
如果div有一个孩子,那就是锚点了.不想在.box中添加其他类
就像是:
$('.box').click(function(){
if ($(this).children('a')) {
//some thing
} else {
//some thing else
}
});
<div class="box"><a href="#"><img src="#" /></a></div>
<div class="box"><img src="#" /></div>
<div class="box"><img src="#" /></div>
Run Code Online (Sandbox Code Playgroud)
检查length财产:
$('.box').click(function(){
if ($(this).children('a').length) {
//some thing
} else {
//some thing else
}
});
Run Code Online (Sandbox Code Playgroud)
由于该children方法(像大多数jQuery方法一样)返回一个jQuery实例,因此它总是会求值true(所以你总是会在if分支中结束).该length属性返回一个数字,它将评估false是否为0.
| 归档时间: |
|
| 查看次数: |
6285 次 |
| 最近记录: |