'innerText' works in IE, but not in Firefox

前端 未结 15 1406
执念已碎
执念已碎 2020-11-21 05:01

I have some JavaScript code that works in IE containing the following:

myElement.innerText = \"foo\";

However, it seems that the \'innerTex

相关标签:
15条回答
  • 2020-11-21 05:36

    found this here:

    <!--[if lte IE 8]>
        <script type="text/javascript">
            if (Object.defineProperty && Object.getOwnPropertyDescriptor &&
                !Object.getOwnPropertyDescriptor(Element.prototype, "textContent").get)
              (function() {
                var innerText = Object.getOwnPropertyDescriptor(Element.prototype, "innerText");
                Object.defineProperty(Element.prototype, "textContent",
                  { // It won't work if you just drop in innerText.get
                    // and innerText.set or the whole descriptor.
                    get : function() {
                      return innerText.get.call(this)
                    },
                    set : function(x) {
                      return innerText.set.call(this, x)
                    }
                  }
                );
              })();
        </script>
    <![endif]-->
    
    0 讨论(0)
  • 2020-11-21 05:39

    This has been my experience with innerText, textContent, innerHTML, and value:

    // elem.innerText = changeVal;  // works on ie but not on ff or ch
    // elem.setAttribute("innerText", changeVal); // works on ie but not ff or ch
    // elem.textContent = changeVal;  // works on ie but not ff or ch
    // elem.setAttribute("textContent", changeVal);  // does not work on ie ff or ch
    // elem.innerHTML = changeVal;  // ie causes error - doesn't work in ff or ch
    // elem.setAttribute("innerHTML", changeVal); //ie causes error doesn't work in ff or ch
       elem.value = changeVal; // works in ie and ff -- see note 2 on ch
    // elem.setAttribute("value", changeVal); // ie works; see note 1 on ff and note 2 on ch
    

    ie = internet explorer, ff = firefox, ch = google chrome. note 1: ff works until after value is deleted with backspace - see note by Ray Vega above. note 2: works somewhat in chrome - after update it is unchanged then you click away and click back into the field and the value appears. The best of the lot is elem.value = changeVal; which I did not comment out above.

    0 讨论(0)
  • 2020-11-21 05:41

    As in 2016 from Firefox v45, innerText works on firefox, take a look at its support: http://caniuse.com/#search=innerText

    If you want it to work on previous versions of Firefox, you can use textContent, which has better support on Firefox but worse on older IE versions: http://caniuse.com/#search=textContent

    0 讨论(0)
  • 2020-11-21 05:43

    Firefox uses the W3C-compliant textContent property.

    I'd guess Safari and Opera also support this property.

    0 讨论(0)
  • 2020-11-21 05:46

    What about something like this?

    //$elem is the jQuery object passed along.
    
    var $currentText = $elem.context.firstChild.data.toUpperCase();
    

    ** I needed to make mine uppercase.

    0 讨论(0)
  • 2020-11-21 05:49

    Update: I wrote a blog post detailing all the differences much better.


    Firefox uses W3C standard Node::textContent, but its behavior differs "slightly" from that of MSHTML's proprietary innerText (copied by Opera as well, some time ago, among dozens of other MSHTML features).

    First of all, textContent whitespace representation is different from innerText one. Second, and more importantly, textContent includes all of SCRIPT tag contents, whereas innerText doesn't.

    Just to make things more entertaining, Opera - besides implementing standard textContent - decided to also add MSHTML's innerText but changed it to act as textContent - i.e. including SCRIPT contents (in fact, textContent and innerText in Opera seem to produce identical results, probably being just aliased to each other).

    textContent is part of Node interface, whereas innerText is part of HTMLElement. This, for example, means that you can "retrieve" textContent but not innerText from text nodes:

    var el = document.createElement('p');
    var textNode = document.createTextNode('x');
    
    el.textContent; // ""
    el.innerText; // ""
    
    textNode.textContent; // "x"
    textNode.innerText; // undefined
    

    Finally, Safari 2.x also has buggy innerText implementation. In Safari, innerText functions properly only if an element is neither hidden (via style.display == "none") nor orphaned from the document. Otherwise, innerText results in an empty string.

    I was playing with textContent abstraction (to work around these deficiencies), but it turned out to be rather complex.

    You best bet is to first define your exact requirements and follow from there. It is often possible to simply strip tags off of innerHTML of an element, rather than deal with all of the possible textContent/innerText deviations.

    Another possibility, of course, is to walk the DOM tree and collect text nodes recursively.

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