.InnerHTML Not working properly in Internet Explorer

后端 未结 10 1502
一个人的身影
一个人的身影 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:17

    What exactly are you trying to achieve, as your end goal? As said previously a link tag should really only appear in the <head> of an html document.

    JavaScript can be a tricky thing in its vanilla flavour, you're better off using a framework, my personal favourite is MooTools though I hear JQuery is pretty good, but MooTools has OOP (for real programmers - tehe).

    This'll allow you to do a lot more, and probably get to your end goal, if you let me / us know then you should get a more direct answer to your issue.

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

    For starters, you are missing an href attribute on your <link>.

    <link href=\"http://test.com/css/template.css\" rel=\"stylesheet\" />
    

    And don't put link and style tags into the <body>. Inject them into the <head>

      var link = document.createElement("link");
      link.href = "http://test.com/css/template.css";
      link.rel = "StyleSheet";
      document.head.appendChild(link);
    
    0 讨论(0)
  • 2020-12-19 09:25

    innerHTML won't work in IE, but using DOM methods it will:

    function getValue()
    {
      var x=document.getElementById("myHeader")
        , link = document.createElement('link')
        , div = document.createElement('div');
      x.innerHTML = '';
      x.appendChild(link);
      x.appendChild(div);
      div.innerHTML = 'abc';
      link.href = 'http://test.com/css/template.css';
      link.rel = 'stylesheet';
      alert(x.innerHTML);
    }
    

    Although the reference states a link tag may only appear in the header, interesting enough the style link does work if you use the code I provided, in all browsers I tried (IE, firefox, chrome). See this jsfiddle (where I used a real css-href from test.com ;-)

    If you however want to place the link in it's rightful section (<head>), use:

    var header = document.getElementsByTagName('head')[0];
      , link = document.createElement('link');
    header.appendChild(link);
    link.href = 'http://test.com/css/template.css';
    link.rel = 'stylesheet';
    
    0 讨论(0)
  • 2020-12-19 09:29

    The thing is it works in FireFox and Google Chrome, but not in IE.

    This is, because you cannot set the innerHTML tag of InternetExplorer with a string, if the string is more than text, ie a HTML element.

    I experienced this trying to dynamically populate a ComboBox into a table cell with AJAX...

    The solution is to use JQuery.
    Then you can do:
    $("#YourElementID").html('<link \"http://test.com/css/template.css\" rel=\"stylesheet\" /> <div>abc</div>');
    and JQuery will do the DOM stuff, that selbie and KooiInc describe, for you.

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

    to the set text properly, i would recommend you to go what i generally do. if you have ie 8 then open the html in ie. press f12 to show the developer tool. now in the new window go to script tag. set break point from where your javascript starts. now press on the button start debugging. execute the js function from your by some event or the way it executes. now on the object in the javascripg function, when the break point hits, right click and add watch. expand the object and see, where your earlier text was and where is your new text.

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

    try

    document.getElementById('tblList').outerHTML="ypur html";
    
    0 讨论(0)
提交回复
热议问题