-2

我有一张我想替换的图片,但是...

<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">" );

谁能告诉我代码有什么问题?

4

4 回答 4

6

不要replacewith用来替换图像。只需更改src

$("selector for img").attr("src", "/content/newimage.jpg");
于 2013-10-20T18:20:22.463 回答
0

你在 double cote 里面使用 double cote ,你应该做的是:

$( "img.attachment-post-thumbnail" ).replaceWith('<img src="/content/newimage.png" class="attachment-post-thumbnail" height="41" width="600">')
于 2013-10-20T18:22:06.417 回答
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">');
于 2013-10-20T18:23:12.073 回答
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');
于 2013-10-20T18:20:59.320 回答