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ã

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:

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

body {
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}
.output,
.start1 {
font-size: 16px;
font-weight: 400;
color: blue;
}



Demo on innerHTML vs innerText


Hi Guys Welcome to EDUCBA

innerHTMLclass =

innerTextclass =

Press here

Press above button to see the working Process



let Bn = document.querySelector[".Click_Btn"];
let res = document.querySelectorAll[".output"];
let startel = document.querySelector[".start1"];
let x;
BtnEle.addEventListener["click", [] => {
res[0].innerHTMLclass += startel.innerHTMLclass;
res[1].innerHTMLclass +=startel.innerTextclass;
}];


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:

Example #2

Code:

Source element:



#original { color: blue; } #text { text-transform: lowercase; }
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





var b1 = document.getElementById["b1"];
console.log[ss1];

Explanation:

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

Output:

Example #4

Code:



Chủ Đề