用javascript替换部分文件名 [英] Replace part of filename with javascript
本文介绍了用javascript替换部分文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想基于屏幕大小加载不同的图像,并且想用javascript替换文件名中的文本会优雅地做到这一点,只要我有相应的图像正确命名。我很接近这个,但是关闭不会削减它。
I want to load different images based on screensize, and thought replacing text in a filename with javascript would do the trick elegantly, as long as I've got corresponding images properly named. I'm close with this, but close won't cut it.
Javascript:
Javascript:
if ($(window).width() > 960) {
$("div:contains(_small.jpg)").html(function (i, currentVal) {
return currentVal.replace("_small.jpg", "_large.jpg");
});
}
HTML:
<img src="...test-image_small.jpg" />
正如你可以在我的小提琴中看到的那样,我已经开始研究文本,但不是文件名。
As you can see in my fiddle, i've got it working on text, but not the filename.
ps - 我特意不想要在媒体查询类中将图像作为背景图像加载。
ps - I specifically don't want to load images as background-image in media-queried classes.
推荐答案
完整功能:
if ($(window).width() > 960) {
$("img[src$='_small.jpg']").each(function() {
var new_src = $(this).attr("src").replace('_small', '_large');
$(this).attr("src", new_src);
});
} else {
alert('Less than 960');
}
这篇关于用javascript替换部分文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文