【发布时间】:2017-12-22 18:05:13
【问题描述】:
正如标题所说,我在使用 jquery 将图像转换为链接时遇到了一些问题。我现在的代码是:
var all_img = $(".message .content").find("img");
$.each(all_img, function (index, value) {
var src = value.src;
value.replaceWith($("<a href='" + src +"'>Image " + index+1 + "</a>"));
});
这会导致图像被[object Object] 替换。我也试过:
$.each(all_img, function (index, value) {
var src = value.src;
value.replaceWith("<a href='" + src +"'>Image " + index+1 + "</a>");
});
这会导致我尝试插入的 html 以纯文本的形式进入。我是否误解了.replaceWith() 的工作原理?
【问题讨论】:
-
请同时发布您的 HTML。我们喜欢minimal reproducible example
标签: javascript jquery html