开发者

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");
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜