Replace innerHTML of all divs with same class
I want to replace the innerHTML of all divs with class "count" with: items1.inne开发者_运维问答rHTML.
How can I do this?
Here you go:
var items = document.getElementById( 'items' ),
divs = document.getElementsByClassName( 'count' );
[].slice.call( divs ).forEach(function ( div ) {
div.innerHTML = items.innerHTML;
});
Live demo: http://jsfiddle.net/MGqGe/
I use this [].slice.call( divs )
to transform the NodeList into a regular array, so that I can call the forEach
method on it.
Btw, careful with innerHTML. I personally would use a library (like jQuery) to clone the contents instead.
you can do this by jQuery this way
$("div.count").html("new content");
精彩评论