Hướng dẫn innerhtml vs innertext

Warning: This element contains code and strong language.

Các ví dụ dưới đây đề cập đến đoạn mã HTML sau :Nút sẽ được tham chiếu bởi JavaScript sau :

var x = document.getElementById('test');

element.innerHTML

Đặt hoặc lấy cú pháp HTML miêu tả thành phần con của thành phần

x.innerHTML
// => "
// =>   Warning: This element contains code and strong language.
// => "

Đây là một phần của Đặc điểm phân tích cú pháp DOM và Serialization của W3C. Lưu ý rằng đó là một thuộc tính của Elementcác đối tượng.

node.innerText

Đặt hoặc lấy văn bản giữa thẻ mở màn và thẻ kết thúc của đối tượng người tiêu dùng người dùng

x.innerText
// => "Warning: This element contains code and strong language."
  • innerTextđược giới thiệu bởi Microsoft và đã được Firefox hỗ trợ trong một thời gian. Vào tháng 8 năm 2016, innerTextđược thông qua bởi WHATWG và được thêm vào Firefox trong phiên bản v45.
  • innerText cung cấp cho bạn nhận thức về phong cách, biểu diễn của văn bản cố gắng khớp với những gì được trình duyệt hiển thị, điều này có nghĩa là:
    • innerTextáp dụng text-transformwhite-spacequy tắc
    • innerText cắt bớt khoảng trắng giữa các dòng và thêm ngắt dòng giữa các mục
    • innerText sẽ không trả lại văn bản cho các mục ẩn
  • innerTextsẽ trả về textContentcho các phần tử không bao giờ được hiển thị như
    và `
  • Thuộc tính của Nodecác phần tử

node.textContent

Lấy hoặc đặt nội dung văn bản của một nút và con cháu của nó .

x.textContent
// => "
// =>   Warning: This element contains code and strong language.
// => "

Mặc dù đây là tiêu chuẩn W3C, nhưng nó không được tương hỗ bởi IE < 9 .

  • Không biết về kiểu dáng và do đó sẽ trả về nội dung bị ẩn bởi CSS
  • Không kích hoạt chỉnh lại (do đó hiệu suất cao hơn)
  • Thuộc tính của Nodecác phần tử

node.value

Điều này phụ thuộc vào yếu tố mà bạn đã nhắm mục tiêu. Đối với ví dụ trên, xtrả về một đối tượng HTMLDivElement, đối tượng này không có thuộc valuetính được xác định.

x.value // => null

Ví dụ: thẻ đầu vào ( ) xác định một thuộc valuetính, tham chiếu đến “giá trị hiện tại trong điều khiển”.


 

Từ những tài liệu :

Lưu ý: đối với một số loại đầu vào nhất định, giá trị trả về có thể không khớp với giá trị mà người dùng đã nhập. Ví dụ: nếu người dùng nhập một giá trị không phải số vào một , giá trị trả về có thể là một chuỗi trống.

Tập lệnh mẫu

Dưới đây là một ví dụ hiển thị đầu ra cho HTML được trình diễn ở trên :

var properties = ['innerHTML', 'innerText', 'textContent', 'value'];

// Writes to textarea#output and console
function log(obj) {
  console.log(obj);
  var currValue = document.getElementById('output').value;
  document.getElementById('output').value = (currValue ? currValue + '\n' : '') + obj; 
}

// Logs property as [propName]value[/propertyName]
function logProperty(obj, property) {
  var value = obj[property];
  log('[' + property + ']'  +  value + '[/' + property + ']');
}

// Main
log('=============== ' + properties.join(' ') + ' ===============');
for (var i = 0; i < properties.length; i++) {
  logProperty(document.getElementById('test'), properties[i]);
}

Warning: This element contains code and strong language.

Mở rộng đoạn mã

Hướng dẫn innerhtml vs innertext

Difference Between innerText vs innerHTML

The following article provides an outline for innerText vs innerHTML. innerText and innerHTML are the JavaScript properties. innerText property returns plain text and removes the spacing, and has compatibility. innerHtml displays content between labels, including text content and the tags and is supported by all the browsers. Html is a markup language used for the development of web pages. This innerHtml will be presented for replacing selected tags and content.

Head to Head Comparison Between innerText vs innerHTML (Infographics)

Below are the top 9 differences between innerText vs innerHTML:

Hướng dẫn innerhtml vs innertext

Key Difference Between innerText vs innerHTML

Let us discuss some of the major key differences between innerText vs innerHTML:

  • The differences are tackled in which the text is handled. innerText is very similar to the textContent. However, the innerText inserts a string as the input to the element while innerHTML runs and executes the element as HTML content.
  • innerHtml makes to works with HTML rich text, and they don’t perform encode and decode text automatically. innerText renderers text contents while inner HTML parses HTML entities within the element.
  • innerText doesn’t work in all the browsers as it is not valid in Firefox and was later added to Firefox v45.
  • innerHtml are vulnerable to XSS attacks, but this is not the case in InnerText. Instead, using inner Html causes the web browsers to reparse all the available DOM elements inside the HTML code. In other terms, creating a new element and DOM appends it gives better performance.
  • innerText applies style-aware representation and gives style transformation. To all intents, innerText is significantly slower. To go for style awareness, we can use inner Html instead.
  • innerText seeks some information from the layout system to brief how the text is displayed to the user. This makes the app performance go away the rails.

Examples of innerText vs innerHTML

Given below are the examples of innerText vs innerHTML:

So here, we need to extract text from the HTML elements. Here is a quick example we can try this in any chosen editor.

Example #1

Code:






Javascript Properties



Demo on innerHTML vs innerText


Hi Guys Welcome to EDUCBA

innerHTMLclass =

innerTextclass =


Press above button to see the working Process




Explanation:

  • In the above code, we need to extract a text part individually and, in other class, an HTML tag too. So we have written a few JavaScript codes to take out the working process.

Output:

Hướng dẫn innerhtml vs innertext

Example #2

Code:

Source element:




Easiest way
to learn the course
is followed
below.

No Content


Output of TextContent Property:



Output of innerText:


.js:

const original = document.getElementById("original");
const textdemo = document.getElementById("textdemo");
const innerTextdemo = document.getElementById("innerTextdemo");
textdemo.value = original.textContent;
innerTextdemo.value = original.innerText;

Example #3

Code:





Sample Document



This is EDUCBA


This is EDUCBA


This is EDUCBA





Explanation:

  • The above code prints the content in the Text area with the innerText property.

Output:

Hướng dẫn innerhtml vs innertext

Example #4

Code:












Explanation:

  • The above JavaScript snippet shows that when we press the button, the corresponding result with text and HTML tags are displayed.

Output:

Hướng dẫn innerhtml vs innertext

innerText vs innerHTML Comparison Table

Let’s discuss the top comparison between innerText vs innerHTML:

Sr. No innerText  innerHTML
Definition When done to an element, it returns a text, and the text part is wrapped by the element meanwhile ignores a space. A good thing is text is selected and copied off the page(stringified selection). innerHTML returns or sets the text part along with the HTML tags, and the spacing is considered. They treat inner elements of HTML by taking attributes as a feature.
Developed innerText was introduced by Microsoft, and it is not a standard. Introduced by Microsoft. The characters (&) ,(< ,>) returned as HTML entities.
Syntax a.innerText a.innerHtml
CSS It doesn’t return the text that is hidden with the CSS element. Once it is set, character data is generated. Doesn’t include opening and closing tags. It returns the text embedded within the CSS element. But here, character data alone is not generated. It does include opening and closing tags.
HTML tags HTML tags cannot be inserted. They are very similar to text content. HTML tags can be inserted into the code. They are not similar to text content.
Key part of code browser.DomElement(“//div [@id=’mydemo’]”). GetProperty(“innerText”) browser.DomElement(“//div [@id=’mydemo’]”). GetProperty(“innerHtml”)
Advantage Here an attribute returns the text content and its descendants, which precisely represents exactly how the text content appears on the page. It is a useful and needed feature in jQuery. When we need to add attributes to the element, inner Html is considered to be cleaner.
Disadvantages It requires a few layouts of system information when returning the text, which decreases the performance. Requires expensive Knowledge of Layout. It has cross-site security attacks. The data are used only; it comes from a trusted database.
Example  


Hello web developersShiv Om How you doing?


let p = document.getElementById(‘CourseInstructor’);

console.log(p.innerText)

Output:

Hello web developers, Shiv How you are doing?


Hello web developers, Shiv Om How you doing?


let p = document.getElementById(‘ preminum”>’);

console.log(p.innerText)

Output:


Hello web developers Shiv How you doing?


Conclusion

Therefore, in this article, we have seen the differences between inner Html and innerText when we are manipulating the JavaScript code. This article highlighted a comparison, and also, you will get an idea what is the working process when you select content using innerText and innerHtml in JavaScript code. To conclude, web development services rendering any web pages and introducing new content to their application need a basic concept.

This is a guide to innerText vs innerHTML. Here we discuss key differences with infographics, comparison tables and examples, respectively. You may also have a look at the following articles to learn more –

  1. HTML vs XML
  2. XHTML vs HTML5
  3. HTML vs HTM
  4. HTML vs JavaScript