我想弄清楚如何转换用户输入的字符串,其中包含一个图像url,通过添加img attr显示为图像。
这是我一直在玩的:
$(".message_content").each(function() {
var text = $(this).text();
text = text.replace("http://www.example.org/myimage.jpg", "<img src="http://www.example.org/myimage.jpg"> ");
$(this).text(text);
});
虽然这不起作用,有人尝试过类似的东西吗?
转义"
.
text = text.replace("http://www.example.org/myimage.jpg",
"<img src="http://www.example.org/myimage.jpg"> ");
一般:
var userinput = "http://www.example.org/myimage.jpg";
text = text.replace(userinput, "<img src="" + userinput + "">");