Get data in div in js

Recently, I encountered the requirement to obtain the content of div. After I went to the console to print it, I found that many attributes can obtain the content. However, considering the compatibility and standardization of the code, I went to check the correct way to obtain it.

 

The following is my verification summary:

①document.getelementbyid("ddhdh").innerHTML can get all the data in the div, including tags
②document.getelementbyid("ddhdh").innerTEXT can get the text data in the div, but not the tags
③document.getElementById(" text”).textContent for fetching data in Firefox 

 

Correction here, there are many summaries on the Internet that are the first way, in fact, this is a wrong approach. innerHTML has no value, which is reasonable browser practice. The normal programming idea is not to take the value of innerHTML, which involves compatibility before ie8

 

The above tags are based on the browsers of the two browser kernels, and these methods are not compatible. Below is the solution

 

if(navigator.appName.indexOf(“Explorer”) > -1){
     var text = document.getElementById ("text"). innerText;
}else{
    var text = document.getElementById(“text”).textContent;
}

 

 

The retrieval indexOf method returns an integer value that indicates the starting position of the substring within the String object. Returns -1 if no substring is found.

There is another method: directly check whether Firefox, if you can't get textContent, it is IE browser

 

if(document.getElementById(“text”).textContent){
    var text = document.getElementById(“text”).textContent;
}else{
     var text = document.getElementById ("text"). innerText;
}

 

 

 

 

 

 

Guess you like

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