开发者

jQuery Replace Value in Div

I have some code like

var newName = 'Tom';
<div id="me"&开发者_JAVA百科gt;My name is {Name}</div>

How can I use jQuery to find and replace the {Name} with the var newName ?


One-off solution:

var newName = 'Tom';
$('#me').text(function (oldText)
{
    return oldText.replace('{Name}', newName);
});

Reusable solution: use an existing templating implementation such as $.tmpl() or mustache.js.


var newText = "Tom";

newText = $("#me").text().replace("{Name}", newText );

$("me").text(newText);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜