用HTML元素替换文本

iLe*_*evi 6 html javascript jquery replace

如何用HTML对象替换特定文本?

例:

var text = "some text to replace here.... text text text";

var element = $('<img src="image">').event().something...

function ReplaceWithObject(textSource, textToReplace, objectToReplace);
Run Code Online (Sandbox Code Playgroud)

所以我想得到这个:

 "some text to replace < img src...etc >.... text text text"
Run Code Online (Sandbox Code Playgroud)

我想操纵对象元素而不用再调用$()方法.

更新: 我解决了.

thanx @kasdega,我在你的脚本中创建了一个新脚本,因为在你的脚本中我无法在替换后修改"元素".这是脚本:

$(document).ready(function() {
    var text = "some text to replace here.... text text text";
    var element = $('<img />');

    text = text.split('here');
    $('.result').append(text[0],element,text[1]);
$(element).attr('src','http://bit.ly/mtUXZZ');
    $(element).width(100);
});
Run Code Online (Sandbox Code Playgroud)

我不知道append方法接受多个元素.这就是想法,只需要自动化多次替换

thanx to all,这里是jsfiddle

Par*_*rma -2

您可以使用 $(selector).outerHtml 获取元素的 html 字符串