Làm cách nào để đặt HTML bằng JavaScript?

Hôm nay, chúng ta sẽ xem xét bốn kỹ thuật khác nhau mà bạn có thể sử dụng để lấy và đặt văn bản cũng như HTML trong các phần tử DOM

Show

Nào cùng đào vào bên trong

Tài sản Element.innerHTML

Bạn có thể sử dụng thuộc tính Element.innerHTML để lấy và đặt nội dung HTML bên trong một phần tử dưới dạng chuỗi

<div class="greeting">
	<p>Hello world!p>
div>

let greeting = document.querySelector('.greeting');

// Get HTML content
// returns "

Hello world!

"
let html = greeting.innerHTML; // Set HTML content // This replaces what was in there already greeting.innerHTML = 'We can dynamically change the HTML. We can even include HTML elements like this link.'; // Add HTML to the end of an element's existing content greeting.innerHTML += ' Add this after what is already there.'; // Add HTML to the beginning of an element's existing content greeting.innerHTML = 'We can add this to the beginning. ' + elem.innerHTML; // You can inject entire elements into other ones, too greeting.innerHTML += '

A new paragraph

'
;

Đây là bản demo của tài sản Element.innerHTML

Bạn có thể sử dụng thuộc tính

let greeting = document.querySelector('.greeting');

// Get HTML content
// returns "

Hello world!

"
let html = greeting.innerHTML; // Set HTML content // This replaces what was in there already greeting.innerHTML = 'We can dynamically change the HTML. We can even include HTML elements like this link.'; // Add HTML to the end of an element's existing content greeting.innerHTML += ' Add this after what is already there.'; // Add HTML to the beginning of an element's existing content greeting.innerHTML = 'We can add this to the beginning. ' + elem.innerHTML; // You can inject entire elements into other ones, too greeting.innerHTML += '

A new paragraph

'
;
1 để lấy và đặt nội dung HTML bao gồm một phần tử. Điều này hoạt động giống như Element.innerHTML, nhưng bao gồm chính phần tử đó khi nhận và cập nhật nội dung HTML

<div class="greeting">
	<p>Hello world!p>
div>

let greeting = document.querySelector('.greeting');

// Get HTML content
// returns "

Hello world!

"
let html = greeting.outerHTML; // Set HTML content // This completely replaces the
element and all of its content
greeting.outerHTML = '

Goodbye, friend! Click here to leave.'; // Add HTML after the element (and outside of it) greeting.outerHTML += ' Add this after what is already there.'; // Add HTML before the element (and outside of it) greeting.outerHTML = 'We can add this to the beginning. ' + greeting.innerHTML;

Đây là bản demo của tài sản

let greeting = document.querySelector('.greeting');

// Get HTML content
// returns "

Hello world!

"
let html = greeting.innerHTML; // Set HTML content // This replaces what was in there already greeting.innerHTML = 'We can dynamically change the HTML. We can even include HTML elements like this link.'; // Add HTML to the end of an element's existing content greeting.innerHTML += ' Add this after what is already there.'; // Add HTML to the beginning of an element's existing content greeting.innerHTML = 'We can add this to the beginning. ' + elem.innerHTML; // You can inject entire elements into other ones, too greeting.innerHTML += '

A new paragraph

'
;
1

Bạn có thể sử dụng thuộc tính

let greeting = document.querySelector('.greeting');

// Get HTML content
// returns "

Hello world!

"
let html = greeting.innerHTML; // Set HTML content // This replaces what was in there already greeting.innerHTML = 'We can dynamically change the HTML. We can even include HTML elements like this link.'; // Add HTML to the end of an element's existing content greeting.innerHTML += ' Add this after what is already there.'; // Add HTML to the beginning of an element's existing content greeting.innerHTML = 'We can add this to the beginning. ' + elem.innerHTML; // You can inject entire elements into other ones, too greeting.innerHTML += '

A new paragraph

'
;
5 để lấy và đặt văn bản của một phần tử (và bỏ qua phần đánh dấu) dưới dạng một chuỗi

Trong ví dụ bên dưới, bạn có thể nhận thấy rằng thuộc tính

let greeting = document.querySelector('.greeting');

// Get HTML content
// returns "

Hello world!

"
let html = greeting.innerHTML; // Set HTML content // This replaces what was in there already greeting.innerHTML = 'We can dynamically change the HTML. We can even include HTML elements like this link.'; // Add HTML to the end of an element's existing content greeting.innerHTML += ' Add this after what is already there.'; // Add HTML to the beginning of an element's existing content greeting.innerHTML = 'We can add this to the beginning. ' + elem.innerHTML; // You can inject entire elements into other ones, too greeting.innerHTML += '

A new paragraph

'
;
5 nhận tất cả nội dung văn bản, bao gồm các thuộc tính CSS bên trong phần tử
let greeting = document.querySelector('.greeting');

// Get HTML content
// returns "

Hello world!

"
let html = greeting.innerHTML; // Set HTML content // This replaces what was in there already greeting.innerHTML = 'We can dynamically change the HTML. We can even include HTML elements like this link.'; // Add HTML to the end of an element's existing content greeting.innerHTML += ' Add this after what is already there.'; // Add HTML to the beginning of an element's existing content greeting.innerHTML = 'We can add this to the beginning. ' + elem.innerHTML; // You can inject entire elements into other ones, too greeting.innerHTML += '

A new paragraph

'
;
8 và phần tử giao diện người dùng
let greeting = document.querySelector('.greeting');

// Get HTML content
// returns "

Hello world!

"
let html = greeting.innerHTML; // Set HTML content // This replaces what was in there already greeting.innerHTML = 'We can dynamically change the HTML. We can even include HTML elements like this link.'; // Add HTML to the end of an element's existing content greeting.innerHTML += ' Add this after what is already there.'; // Add HTML to the beginning of an element's existing content greeting.innerHTML = 'We can add this to the beginning. ' + elem.innerHTML; // You can inject entire elements into other ones, too greeting.innerHTML += '

A new paragraph

'
;
9

Bất kỳ phần tử HTML nào được bao gồm trong một chuỗi khi đặt nội dung với thuộc tính

let greeting = document.querySelector('.greeting');

// Get HTML content
// returns "

Hello world!

"
let html = greeting.innerHTML; // Set HTML content // This replaces what was in there already greeting.innerHTML = 'We can dynamically change the HTML. We can even include HTML elements like this link.'; // Add HTML to the end of an element's existing content greeting.innerHTML += ' Add this after what is already there.'; // Add HTML to the beginning of an element's existing content greeting.innerHTML = 'We can add this to the beginning. ' + elem.innerHTML; // You can inject entire elements into other ones, too greeting.innerHTML += '

A new paragraph

'
;
5 sẽ tự động được mã hóa và hiển thị nguyên trạng

<div class="greeting">
	<style type="text/css">
		p {
			color: rebeccapurple;
		}
	style>
	<p hidden>This is not rendered.p>
	<p>Hello world!p>
div>

let greeting = document.querySelector('.greeting');

// Get text content
// returns "p {color: rebeccapurple;} This is not rendered. Hello world!"
let text = greeting.textContent;

// Set text content
// This completely replaces whats there, including any HTML elements
greeting.textContent = 'We can dynamically change the content.';

// Add text to the end of an element's existing content
greeting.textContent += ' Add this after what is already there.';

// Add text to the beginning of an element's existing content
greeting.textContent = 'We can add this to the beginning. ' + greeting.textContent;

// HTML elements are automatically encoded and rendered as-is
greeting.textContent = '

See you later!

'
;

Đây là bản demo của tài sản

let greeting = document.querySelector('.greeting');

// Get HTML content
// returns "

Hello world!

"
let html = greeting.innerHTML; // Set HTML content // This replaces what was in there already greeting.innerHTML = 'We can dynamically change the HTML. We can even include HTML elements like this link.'; // Add HTML to the end of an element's existing content greeting.innerHTML += ' Add this after what is already there.'; // Add HTML to the beginning of an element's existing content greeting.innerHTML = 'We can add this to the beginning. ' + elem.innerHTML; // You can inject entire elements into other ones, too greeting.innerHTML += '

A new paragraph

'
;
5

Tài sản

Hello world!

2

Thuộc tính

<div class="greeting">
	<p>Hello world!p>
div>
2 lấy và đặt văn bản được hiển thị của một phần tử (và bỏ qua phần đánh dấu)

Không giống như thuộc tính

let greeting = document.querySelector('.greeting');

// Get HTML content
// returns "

Hello world!

"
let html = greeting.innerHTML; // Set HTML content // This replaces what was in there already greeting.innerHTML = 'We can dynamically change the HTML. We can even include HTML elements like this link.'; // Add HTML to the end of an element's existing content greeting.innerHTML += ' Add this after what is already there.'; // Add HTML to the beginning of an element's existing content greeting.innerHTML = 'We can add this to the beginning. ' + elem.innerHTML; // You can inject entire elements into other ones, too greeting.innerHTML += '

A new paragraph

'
;
5, thuộc tính
<div class="greeting">
	<p>Hello world!p>
div>
2 chỉ trả về văn bản được hiển thị, tương tự như những gì người dùng có thể chọn bằng con trỏ hoặc bàn phím khi đánh dấu văn bản

Giống như

let greeting = document.querySelector('.greeting');

// Get HTML content
// returns "

Hello world!

"
let html = greeting.innerHTML; // Set HTML content // This replaces what was in there already greeting.innerHTML = 'We can dynamically change the HTML. We can even include HTML elements like this link.'; // Add HTML to the end of an element's existing content greeting.innerHTML += ' Add this after what is already there.'; // Add HTML to the beginning of an element's existing content greeting.innerHTML = 'We can add this to the beginning. ' + elem.innerHTML; // You can inject entire elements into other ones, too greeting.innerHTML += '

A new paragraph

'
;
5, bất kỳ phần tử HTML nào được bao gồm trong chuỗi khi đặt nội dung sẽ tự động được mã hóa và hiển thị nguyên trạng

<div class="greeting">
	<style type="text/css">
		p {
			color: rebeccapurple;
		}
	style>
	<p hidden>This is not rendered.p>
	<p>Hello world!p>
div>

let elem = document.querySelector('.greeting');

// Get text content
// returns "Hello world!"
let text = elem.innerText;

// Set text content
// This completely replaces whats there, including any HTML elements
elem.innerText = 'We can dynamically change the content.';

// Add text to the end of an element's existing content
elem.innerText += ' Add this after what is already there.';

// Add text to the beginning of an element's existing content
elem.innerText = 'We can add this to the beginning. ' + elem.innerText;

// HTML elements are automatically encoded and rendered as-is
elem.innerText = '

See you later!

'
;

Đây là bản demo của tài sản

<div class="greeting">
	<p>Hello world!p>
div>
2

Bạn nên sử dụng cái nào?

Nói chung, nếu bạn chỉ sửa đổi văn bản, sử dụng thuộc tính

let greeting = document.querySelector('.greeting');

// Get HTML content
// returns "

Hello world!

"
let html = greeting.innerHTML; // Set HTML content // This replaces what was in there already greeting.innerHTML = 'We can dynamically change the HTML. We can even include HTML elements like this link.'; // Add HTML to the end of an element's existing content greeting.innerHTML += ' Add this after what is already there.'; // Add HTML to the beginning of an element's existing content greeting.innerHTML = 'We can add this to the beginning. ' + elem.innerHTML; // You can inject entire elements into other ones, too greeting.innerHTML += '

A new paragraph

'
;
5 là lựa chọn tốt nhất, an toàn nhất của bạn

Để sửa đổi HTML, thuộc tính Element.innerHTML rất hữu ích, nhưng có một số lo ngại về bảo mật mà chúng tôi sẽ xem xét trong một bài viết khác