开发者

jQuery Replace Value in Div

开发者 https://www.devze.com 2023-04-03 09:03 出处:网络
I have some code like var newName = \'Tom\'; <div id=\"me\"&开发者_JAVA百科gt;My name is {Name}</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

精彩评论

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