我有一些jquery,它会在屏幕尺寸低于600像素时替换图像。jquery图像替换屏幕尺寸调整
<script type="text/javascript">
jQuery(window).on("load resize", function (e) {
jQuery(function() {
if (jQuery(window).width() <= 600) {
jQuery("img").each(function() {
jQuery(this).attr("src", jQuery(this).attr("src").replace("img/my_safe_net_hero_img_knight_2014_2.jpg", "img/my_safe_net_knight_mob_version_2014.png"));
});
}
});
});
</script>
这是有效的,但是如果你想再次放大屏幕,小图仍然存在。我想我可以通过添加一个else语句来修复它,但它不起作用。当我这样做的时候,似乎打破了整个事情。
<!--Hero Image Replacement-->
<script type="text/javascript">
jQuery(window).on("load resize", function (e) {
jQuery(function() {
if (jQuery(window).width() <= 600) {
jQuery("img").each(function() {
jQuery(this).attr("src", jQuery(this).attr("src").replace("img/my_safe_net_hero_img_knight_2014_2.jpg", "img/my_safe_net_knight_mob_version_2014.png"));
});
}
else {
jQuery(this).attr("src", jQuery(this).attr("src").replace("img/my_safe_net_knight_mob_version_2014.png" , "img/my_safe_net_hero_img_knight_2014_2.jpg"));
};
});
});
</script>
上面的脚本有什么问题。我应该使用不同的东西来达到这个目的吗?
这里是一个Fiddle of my code
在其他方面,你没有循环使用'img'。 – 2014-10-02 14:13:33