innerText compatible handling

Reprinted from: https://www.cnblogs.com/leejersey/p/3520497.html; slightly changed and added some comments;

IE, Safari, Opera and Chrome support the innerText property. Although Firefox does not support innerText, it does support a similar textContent property. textContent is an attribute specified at DOM3 level and is also supported by Safari, Opera and Chrome. In order to ensure cross-browser compatibility, it is necessary to use the following function to detect which attribute can be used: (Note: corresponding to innerHTML {this does not seem to need to be handled concurrently and the processing of each mainstream browser kernel is the same} , and jQuery's val refers to the value of the value attribute of elem)

  var div = document.getElementById("content");
function getInnerText(element) { 
  // First judge whether there is a textContent property in the obtained element, if not, it will be "undefined" return ( typeof element.textContent === "string") ? element.textContent : element.innerText; } function setInnerText(element, text) { if ( typeof element.textContent === "string" ) { element.textContent = text; } else { element.innerText = text; } } setInnerText(elem, "Hello world!" ) ; alert(getInnerText(elem)); // "Hello world!"

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324771252&siteId=291194637