-7

I am building a HTML site & that site has too many pages. So I want a code so that I can edit a particular div from my website so it will apply to all over in my website. For eg. In HTML >>

<div id="special-id"> I want to change or place anything [HTML code also] inside of this div with javascript </div>`*

& in javascript >>

<script type="text/javascript">
????
</script>

Please need help so that I can move to my HTML website again.

I want to change a content that may be simple text or a javascript code or HTML code?

4

2 回答 2

3
document.getElementById('special-id').innerHTML = "Whatever you want to replace with";
于 2013-07-07T12:21:01.410 回答
1

The easy way: first, get your element with e.g. getElementById, then set innerHTML to your new code.

var div = document.getElementById('special-id');
div.innerHTML = '<span>hello world</span>';

The DOM method only way: create your new HTML nodes using document.createElement or document.createTextNode and then append them to your element with appendChild.

// assuming `div` as above
var newNode = document.createElement('span');
span.appendChild(
    document.createTextNode('hello world')
);
div.appendChild(span);
于 2013-07-07T12:21:11.300 回答