我有一个我想替换的图片但是......
<div class="news-thumb-wrapper">
<img src="/content/oldimage.jpg" class="attachment-post-thumbnail" height="150" width="600">
<div class="gallery-arrows"></div><h8><a href="/">Some text</a></h8></div>
我已经使用此代码尝试替换整个img标记,但它似乎不起作用。
$( "img.attachment-post-thumbnail" ).replaceWith( "<img src="/content/newimage.png" class="attachment-post-thumbnail" height="41" width="600">" );
有人可以告诉我这段代码有什么问题吗?
答案 0 :(得分:5)
请勿使用replacewith
替换图像。只需更改src
:
$("selector for img").attr("src", "/content/newimage.jpg");
答案 1 :(得分:0)
您的代码语法无效。你应该转义字符串中的双引号或使用单引号:
$( "img.attachment-post-thumbnail" ).replaceWith( '<img src="/content/newimage.png" class="attachment-post-thumbnail" height="41" width="600">' );
现在,交换图像的正确方法是简单地设置其src
属性:
$( "img.attachment-post-thumbnail" ).prop('src', '/content/newimage.png');
答案 2 :(得分:0)
你在双引号内使用双引号,你应该做的是:
$( "img.attachment-post-thumbnail" ).replaceWith('<img src="/content/newimage.png" class="attachment-post-thumbnail" height="41" width="600">')
答案 3 :(得分:0)
好吧,您可以尝试转义引号,例如"<img src=\"/content/newimage.png\" class=\"attachment-post-thumbnail\" height=\"41\" width=\"600\">"
$( "img.attachment-post-thumbnail" ).replaceWith( "<img src=\"/content/newimage.png\" class=\"attachment-post-thumbnail\" height=\"41\" width=\"600\">" );
或类似'<img src="/content/newimage.png" class="attachment-post-thumbnail" height="41" width="600">'
$( "img.attachment-post-thumbnail" ).replaceWith('<img src="/content/newimage.png" class="attachment-post-thumbnail" height="41" width="600">');