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);
精彩评论