.InnerHTML Not working properly in Internet Explorer

后端 未结 10 1503
一个人的身影
一个人的身影 2020-12-19 08:55

I wanted to assign a link tag to innerHTML of a HTML control. But this is not working properly in Internet Explorer. However when I try to assign anything other than &

相关标签:
10条回答
  • 2020-12-19 09:39

    Found different and easy solution here for "link" and "sytle", but "script" needs to be added using appendChild. Very much similar to what Vectorjohn has said, also provides more references.

    http://allofetechnical.wordpress.com/2010/05/21/ies-innerhtml-method-with-script-and-style-tags/

    0 讨论(0)
  • 2020-12-19 09:40

    i think the problem is that the <link> hast to be an empty element. change your code to this:

    x.innerHTML='<link \"http://test.com/css/template.css\" rel=\"stylesheet\" /><div>abc</div>';
                                                         // this is the trick ^^^
    

    EDIT: i havn't tested this, but it's the first thing that hurts my eyes.

    EDIT2: <link> tags should only occur inside of the <head>-section... i hope you know what you're trying to do.

    0 讨论(0)
  • 2020-12-19 09:43

    A lot of people are missing the point here. What he is trying to do ( after fixing the typo where the href attribute is missing ) works in any other browser.

    IE 8 and below have a bug where if the first element in the text when setting innerHTML is a tag (maybe others), it is ignored. If you just put a space or newline or other tag first, it works.

    He even discovered this when he said putting the <div> first fixes it.

    It isn't valid, but that's how you fix it.

    Edit: in other words: foo.innerHTML = "\n" + yourHtmlWithLinkInIt;

    0 讨论(0)
  • 2020-12-19 09:44

    <link> can only be contained in <head>.

    This element defines a link. Unlike A, it may only appear in the HEAD section of a document, although it may appear any number of times.

    reference: http://www.w3.org/TR/html401/struct/links.html#edef-LINK

    0 讨论(0)
提交回复
热议问题