gpt4 book ai didi

jquery - 获取宽度大于特殊值的图像

转载 作者:行者123 更新时间:2023-11-28 12:39:55 25 4
gpt4 key购买 nike

我尝试从图像列表中获取宽度大于特殊值的图像,我使用下面的代码,但它不起作用。怎么了?如何让它发挥作用?

$('.carousel .slideItem img').each(function(){
var img_width = $(this).css('width');
console.log(img_width);
if( img_width > 400 ){
var img_src = $(this).attr('src');
console.log(img_src);
}
})

最佳答案

.css('width') 将返回元素的计算宽度,这将是 x 像素(例如 40px)。当您尝试比较它时,JavaScript 会尝试将其转换为整数,导致 img_widthNaN

使用.width()获取以像素为单位的整数宽度:

$('.carousel .slideItem img').each(function () {
var img_width = $(this).width();
console.log(img_width);
if (img_width > 400) {
var img_src = $(this).attr('src');
console.log(img_src);
}
})

关于jquery - 获取宽度大于特殊值的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26626485/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com