gpt4 book ai didi

javascript - 在没有图像扩展名的情况下添加图像扩展名

转载 作者:行者123 更新时间:2023-12-03 06:11:23 26 4
gpt4 key购买 nike

使用以下脚本,我将遍历页面上的每个 img 源,检索扩展名,如果没有返回的字符串,或者不知道图像类型,则添加 .jpg

出于某种原因,无论扩展名如何,if 语句每次都为 true。

$(document).ready(function() {          
$(".main_img").each(function() {
imgsrc = this.src;
console.log(imgsrc);
imgext = imgsrc.split('.').pop();
$.trim(imgext);
console.log(imgext);
if(imgext != 'jpg' || imgext != 'jpeg' || imgext != 'gif'){
console.log("Unacceptable,add extension");
FinalURL = imgsrc + '.jpg';
}
else {
console.log(imgext);
}
});
});

最佳答案

轻松修复代码中的错误。

只需将 OR 条件更改为 AND 条件即可。

更改这部分:

if(imgext != 'jpg' || imgext != 'jpeg' || imgext != 'gif')

对此:

if(imgext != 'jpg' && imgext != 'jpeg' && imgext != 'gif')

它不起作用,因为在这种情况下使用 OR 条件时,imgext 总是不会同时等于 3 个不同的值。因此,它总会实现。使用 AND 时,仅当它不等于三者中任何一个的值时才会接受。应该可以工作。

关于javascript - 在没有图像扩展名的情况下添加图像扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39299916/

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