jquery how to replace live text?
i've got some text that is being loaded into a <span>
from a json file.
the json file has some fields that come as null
and i would like to replace them with something else.
i've een trying this :
var e = $('span.black').text();
e.replace(/\null/g, "test");
but it doesn't seem to work.
Another thing is that i try to replace the text immediately after i load the json file开发者_开发问答.
thanks
I would suggest you change the text right after the JSON is loaded like this:
if (!data.someKey) {
data.someKey = "some default text";
}
$('span.black').text(data.someKey);
or, if you really need to replace the value after it was added to the span:
var elem = $('span.black');
elem.text(elem.text().replace(/null/g, "some default text"));
Given this HTML:
<span id="target">Old Content</span>
The following JQuery code will change the content:
$('#target').html("New Content");
Example @ jsfiddle.net
精彩评论