使用 jQuery 定位 GIF
Target GIFs with jQuery
有没有办法定位所有 GIF src?我只想将 JS 代码应用于 GIF。
这是我目前拥有的代码,但它针对的是 img:
$(document).ready(function(){
$('#banner img').each(function () {
var curSrc = $(this).attr('src');
$(this).attr('src', curSrc.split('?')[0]);
});
});
已编辑:GIF 有参数(这就是我使用上面的代码删除这些参数的原因),所以我无法定位最终字符串,因为它不是 .gif
这是 HTML 的样子:<img src="/asset/News/6001/gif-anim.gif?thumbnail_width=2000&thumbnail_height=500&resize_type=CropToFit" height="500" width="2000" alt="">
您可以检查文件扩展名 indexOf
并找出哪些 img 是 GIF,哪些不是。
$(document).ready(function(){
$('#banner img').each(function () {
var curSrc = $(this).attr('src');
if(curSrc.indexOf('.gif')!=-1 || curSrc.indexOf('.GIF')!=-1) {
$(this).attr('src', curSrc.split('?')[0]);
console.log("It is GIF");
} else {
console.log("It is not GIF");
}
});
});
有没有办法定位所有 GIF src?我只想将 JS 代码应用于 GIF。
这是我目前拥有的代码,但它针对的是 img:
$(document).ready(function(){
$('#banner img').each(function () {
var curSrc = $(this).attr('src');
$(this).attr('src', curSrc.split('?')[0]);
});
});
已编辑:GIF 有参数(这就是我使用上面的代码删除这些参数的原因),所以我无法定位最终字符串,因为它不是 .gif
这是 HTML 的样子:<img src="/asset/News/6001/gif-anim.gif?thumbnail_width=2000&thumbnail_height=500&resize_type=CropToFit" height="500" width="2000" alt="">
您可以检查文件扩展名 indexOf
并找出哪些 img 是 GIF,哪些不是。
$(document).ready(function(){
$('#banner img').each(function () {
var curSrc = $(this).attr('src');
if(curSrc.indexOf('.gif')!=-1 || curSrc.indexOf('.GIF')!=-1) {
$(this).attr('src', curSrc.split('?')[0]);
console.log("It is GIF");
} else {
console.log("It is not GIF");
}
});
});