简体   繁体   中英

IE9, Javascript: Create and append a new element

I'm having some serious trouble getting my code to work in IE9, works fine in Chrome & Firefox but I throws some errors. Here are my 2 functions:

function insertHTML(content){
    var body=document.getElementsByTagName('body');
    body[0].appendChild(createElement(content));
 }

function createElement(string){
     var container=document.createElement('div');
     container.innerHTML=string;
     var element=container.firstChild.cloneNode(true);
     return element;
 }

I've tried severel methods for this and none seem to work, I'll explain exactly what I need to do...

...I need to create a new element from an html string, the string is sent back from an ajax call so my script will have almost no idea what it contains until it gets it.

I did try using element.innerHTML but this is no good, because if i have one html element (form) on the screen and the user enters data into it, and then when another element is inserted it will wipe all the user-entered data from the first form. I was doing element.innerHTML+=newData;

So basically, I need 2 things:

1) A way to create a new element from an html string. 2) A way to append the element to the document body.

It all needs to work cross-browser and I'm not allowed to use jQuery, also the new element cannot be contained in a div parent item, it has to have the body as its parent.

Thanks very much for your help,

Richard

So basically, I need 2 things:

  1. A way to create a new element from an html string.
  2. A way to append the element to the document body.

It all needs to work cross-browser and I'm not allowed to use jQuery, also the new element cannot be contained in a div parent item, it has to have the body as its parent.

The following was tested in IE8

<!DOCTYPE html>
<html>
<body>
<script>
var divBefore = document.createElement('div');
var divAfter = document.createElement('div');
var htmlBefore = '<span><span style="font-weight: bold">This bold text</span> was added before</span>';
var htmlAfter = '<span><span style="font-weight: bold">This bold text</span> was added after</span>';

divBefore.innerHTML = htmlBefore;
divAfter.innerHTML = htmlAfter;

document.body.appendChild(divBefore);

setTimeout(function() {
    document.body.appendChild(divAfter);
}, 0);

</script>
<div>This content was here first</div>
</body>
</html>

Renders

This bold text was added before
This content was here first
This bold text was added after

https://www.browserstack.com/screenshots/7e166dc72b636d3dffdd3739a19ff8956e9cea96


In the above example, if you don't need to be able to prepend to the body (ie insert content before what already exists), then simply place the script tag after the original content and don't use setTimeout .

<!DOCTYPE html>
<html>
<body>
<div>This content was here first</div>
<script>
var divAfter = document.createElement('div');
var htmlAfter = '<span><span style="font-weight: bold">This bold text</span> was added after</span>';

divAfter.innerHTML = htmlAfter;

document.body.appendChild(divAfter);
</script>
</body>
</html>

innerHTML is read write and will destroy anything inside your div. use with extreme care

function insertHTML( htmlString ){
    var bodEle = document.getElementsByTagName('body');
    var divEle = createElement("div");
    divEle.innerHTML = htmlString;
    bodEle.appendChild(divEle);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM