用Wordpress和jQuery替换图像

时间:2013-09-14 18:53:32

标签: jquery image wordpress replace

我的代码不起作用我不知道为什么?我有Wordpress Wordpress插件将一些外部网站的代码注入我的wordpress网站。
代码从外部网站加载并在我的wordpress网站上显示一些丑陋的图像。所以我想用我自己的图像替换丑陋的图像。我无法编辑外部网站的HTML代码。

这是我在footer.php中插入的代码。

<script type="text/javascript">
jQuery(document).ready(function() {
 var the_image_source = "http://original.xyz/original.png";
var new_src = "/wp-content/images/myone.png";
jQuery('img[src=' + the_image_source + ']').attr('src',new_src);
    });
</script>

我不知道出了什么问题,所以请帮助我!谢谢!

1 个答案:

答案 0 :(得分:1)

如果在选择器中添加双引号,它将起作用:

jQuery(document).ready(function () {
    var the_image_source = "http://original.xyz/original.png";
    var new_src = "/wp-content/images/myone.png";
    jQuery('img[src="' + the_image_source + '"]').attr('src', new_src);
//                  ^                        ^  
});

演示here

相关问题