检查 div 里面是否有图片,然后做点什么
check if div has image inside, then do something
如果 div.local_logo
中有 标签,我想将 addClass 添加到上一个 div.local_text
<div>
<div class="local_text"></div>
<div class="local_logo">
<a href="#"></a>
</div>
</div>
到目前为止,这不起作用,正在将 class 添加到所有 div.local_text
,但仅添加前一个。
if (jQuery(".local_logo:has(img)")) {
jQuery(".local_logo").prev(".local_text").addClass("half");
};
我建议:
// cache the <div clas="local_text"> elements:
var text = jQuery('.local_text');
// use toggleClass() with a class-name and switch,
// the class-name will be applied to the relevant elements
// if the switch returns a truthy value (0 is falsey):
text.toggleClass('half', text.next('div.local_logo').find('img').length);
请注意,如果下一个 <div>
包含 <img>
元素,问题标题说您要添加 class,而问题中的代码有 <a>
元素.
如有错误,请在find()
范围内适当调整选择器。
参考文献:
试试这个(使用 .find()
方法)
$('.local_logo div').each(function() {
if ($(this).find('img').length) {
// there is an image in this div, do anything here..
}
});
对我有用,希望也能解决你的问题:)
使用:has()
选择器
Selects elements which contain at least one element that matches the specified selector.
jQuery(".local_logo:has(img)") // Select all the classes `.local_logo` having img as descendent
.prev(".local_text").addClass("half");
jQuery(".local_logo:has(img)").prev(".local_text").addClass("half");
.half {
background: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
<div class="local_text">First</div>
<div class="local_logo">
<img src="" alt="">
</div>
</div>
<div>
<div class="local_text">Second</div>
<div class="local_logo">
<a href="#"></a>
</div>
</div>
<div>
<div class="local_text">Third</div>
<div class="local_logo">
<img src="" alt="">
</div>
</div>
如果 div.local_logo
中有 标签,我想将 addClass 添加到上一个 div.local_text<div>
<div class="local_text"></div>
<div class="local_logo">
<a href="#"></a>
</div>
</div>
到目前为止,这不起作用,正在将 class 添加到所有 div.local_text
,但仅添加前一个。
if (jQuery(".local_logo:has(img)")) {
jQuery(".local_logo").prev(".local_text").addClass("half");
};
我建议:
// cache the <div clas="local_text"> elements:
var text = jQuery('.local_text');
// use toggleClass() with a class-name and switch,
// the class-name will be applied to the relevant elements
// if the switch returns a truthy value (0 is falsey):
text.toggleClass('half', text.next('div.local_logo').find('img').length);
请注意,如果下一个 <div>
包含 <img>
元素,问题标题说您要添加 class,而问题中的代码有 <a>
元素.
如有错误,请在find()
范围内适当调整选择器。
参考文献:
试试这个(使用 .find()
方法)
$('.local_logo div').each(function() {
if ($(this).find('img').length) {
// there is an image in this div, do anything here..
}
});
对我有用,希望也能解决你的问题:)
使用:has()
选择器
Selects elements which contain at least one element that matches the specified selector.
jQuery(".local_logo:has(img)") // Select all the classes `.local_logo` having img as descendent
.prev(".local_text").addClass("half");
jQuery(".local_logo:has(img)").prev(".local_text").addClass("half");
.half {
background: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
<div class="local_text">First</div>
<div class="local_logo">
<img src="" alt="">
</div>
</div>
<div>
<div class="local_text">Second</div>
<div class="local_logo">
<a href="#"></a>
</div>
</div>
<div>
<div class="local_text">Third</div>
<div class="local_logo">
<img src="" alt="">
</div>
</div>