I have an innerHTML of one span (outer) collected in one varaiable.
Now there is one nested span inside span Inner, so can I set the innerHTMl of span text as follows?
var outertext = document.getElementById开发者_如何学Go("outer");
Now outertext has inner span, and I want to replace text of inner span.
How can I do it?
Get the span ;)
outertext.getElementsByTagName('span')[0].innerHTML = 'O_o'
Replace 0
with whatever suits your needs.
If you want to write less, you can use jQuery:
$('#outer > span').html('new content here');
If the span is not a direct child of #outer, remove the >
.
If you don't want to use anything but native functions, see the other two answers.
outertext.getElementsByTagName('span')[0].innerHTML = "NEW CONTENT";
Example: http://jsfiddle.net/JAmFM/
(Note that it is getElementById
. You had getElementbyid
. Maybe a typo.)
If for some reason you do not want to use jQuery, you can do it with pure JavaScript by using querySelector()
:
document.querySelector('#outer > span').innerHTML = "New content here";
精彩评论