Jquery 如果类为';s子图像宽度为<;667
我有一个图像列表,它们在列表标签中。如果图像(位于列表标记内)的宽度小于667px,我想通过jQuery向列表标记添加一个类Jquery 如果类为';s子图像宽度为<;667,jquery,html,css,Jquery,Html,Css,我有一个图像列表,它们在列表标签中。如果图像(位于列表标记内)的宽度小于667px,我想通过jQuery向列表标记添加一个类 jQuery('.basic-gallery li').each(function() { if(jQuery(this).children().attr('width') < 700) { jQuery(this).parent().addClass('half'); }; }); <ul class="basic-gallery text-cen
jQuery('.basic-gallery li').each(function() {
if(jQuery(this).children().attr('width') < 700) {
jQuery(this).parent().addClass('half');
};
});
<ul class="basic-gallery text-center">
<li><img class="attachment-large" width="667" height="1000" src="/example.jpg" style="max-height: 917px; width: auto; height: auto;"></img></li>
<li><img class="attachment-large" width="667" height="1000" src="/example.jpg" style="max-height: 917px; width: auto; height: auto;"></img></li>
<li><img class="attachment-large" width="667" height="1000" src="/example.jpg" style="max-height: 917px; width: auto; height: auto;"></img></li>
<li><img class="attachment-large" width="667" height="1000" src="/example.jpg" style="max-height: 917px; width: auto; height: auto;"></img></li>
jQuery('.basic gallery li')。每个(函数(){
if(jQuery(this).children().attr('width')<700){
jQuery(this.parent().addClass('half');
};
});
如何做到这一点?将
jQuery(this.children().attr('width')
更改为jQuery(this.children().width
,如果您希望从img
标记属性中获得宽度:
$('.basic-gallery li').each(function() {
if($(this).find('img').attr('width') < 667) {
$(this).closest('ul').addClass('half');
};
});
$('.basic gallery li')。每个(函数(){
if($(this).find('img').attr('width')<667){
$(this).closest('ul').addClass('half');
};
});
否则,按照上面@Anri的答案,您可以得到图像的宽度:
$('.basic-gallery li').each(function() {
if($(this).find('img').width() < 667) {
$(this).closest('ul').addClass('half');
};
});
$('.basic gallery li')。每个(函数(){
if($(this).find('img').width()<667){
$(this).closest('ul').addClass('half');
};
});
请看上面我发布的链接