We would like to know how to change text content.
<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
window.onload=function(){<!--from ww w . j av a 2s . c o m-->
function replaceTextInClass(className, text) {
var elms = document.querySelectorAll('.' + className), i;
for (i = 0; i < elms.length; ++i) {
elms[i].textContent = text;
}
}
replaceTextInClass('fruit', 'Oranges');
replaceTextInClass('fish', 'Shark');
}
</script>
</head>
<body>
Favorite things to eat
<span class="fruit">Text will appear</span> Things that grow on trees<br/>
<span class="fruit">Text will appear</span> Dinner<br/>
<span class="fish">Text will appear</span> Creature in the ocean<br/>
<span class="fish">Text will appear</span><br/>
</body>
</html>
The code above is rendered as follows: