Điều này là khó khăn, bởi vì văn bản nằm trong một nút văn bản và jQuery không hỗ trợ các nút văn bản, chỉ các phần tử. Ngoài ra bởi vì các trình duyệt khác nhau xử lý không gian trắng trong mã HTML khác nhau, vì vậy bạn kết thúc với các bộ nút khác nhau trong các trình duyệt khác nhau. Bạn không thể chỉ nhận các nút và dựa vào nút văn bản mà bạn muốn ở cùng một chỉ mục mọi lúc.
Bạn có thể sử dụng jQuery để xác định vị trí một phần tử và sử dụng DOM để lấy tất cả các nút bên trong nó.
Cải thiện bài viết
Lưu bài viết
ĐọcBàn luậnCải thiện bài viết
Lưu bài viết
Đọc
- Bàn luận
- Cho một tài liệu HTML chứa một số phần tử và tác vụ là lấy văn bản bên trong phần tử HTML bằng JavaScript. Có hai phương pháp để có được văn bản mà không có phần tử HTML được liệt kê dưới đây:
Sử dụng thuộc tính InnerText We can use innerText property to get the text from HTML element.
Example:
<!DOCTYPE html>
<
html
var nodes = $('#test')[0].childNodes;
0<
var nodes = $('#test')[0].childNodes;
2var nodes = $('#test')[0].childNodes;
0var nodes = $('#test')[0].childNodes;
4<
var nodes = $('#test')[0].childNodes;
6var nodes = $('#test')[0].childNodes;
0var nodes = $('#test')[0].childNodes;
8var nodes = $('#test')[0].childNodes;
9var nodes = $('#test')[0].childNodes;
8var index;
$.each(nodes, function(i,e){
if (e.tagName == 'STRONG') {
index = i;
return false;
}
});
1var nodes = $('#test')[0].childNodes;
4var index;
$.each(nodes, function(i,e){
if (e.tagName == 'STRONG') {
index = i;
return false;
}
});
3var nodes = $('#test')[0].childNodes;
6var nodes = $('#test')[0].childNodes;
0var index;
$.each(nodes, function(i,e){
if (e.tagName == 'STRONG') {
index = i;
return false;
}
});
3var nodes = $('#test')[0].childNodes;
2var index;
$.each(nodes, function(i,e){
if (e.tagName == 'STRONG') {
index = i;
return false;
}
});
8<
var text = nodes[index + 1].nodeValue;
0var index;
$.each(nodes, function(i,e){
if (e.tagName == 'STRONG') {
index = i;
return false;
}
});
8var nodes = $('#test')[0].childNodes;
4<
var text = nodes[index + 1].nodeValue;
4 var text = nodes[index + 1].nodeValue;
5var text = nodes[index + 1].nodeValue;
6var text = nodes[index + 1].nodeValue;
7var index;
$.each(nodes, function(i,e){
if (e.tagName == 'STRONG') {
index = i;
return false;
}
});
8var nodes = $('#test')[0].childNodes;
8[0]
0var nodes = $('#test')[0].childNodes;
4var index;
$.each(nodes, function(i,e){
if (e.tagName == 'STRONG') {
index = i;
return false;
}
});
3var text = nodes[index + 1].nodeValue;
4var index;
$.each(nodes, function(i,e){
if (e.tagName == 'STRONG') {
index = i;
return false;
}
});
8var nodes = $('#test')[0].childNodes;
4<
[0]
7var index;
$.each(nodes, function(i,e){
if (e.tagName == 'STRONG') {
index = i;
return false;
}
});
8var nodes = $('#test')[0].childNodes;
8childNodes
0var nodes = $('#test')[0].childNodes;
8childNodes
2var nodes = $('#test')[0].childNodes;
4var index;
$.each(nodes, function(i,e){
if (e.tagName == 'STRONG') {
index = i;
return false;
}
});
3[0]
7var index;
$.each(nodes, function(i,e){
if (e.tagName == 'STRONG') {
index = i;
return false;
}
});
8var index;
$.each(nodes, function(i,e){
if (e.tagName == 'STRONG') {
index = i;
return false;
}
});
3var text = nodes[index + 1].nodeValue;
0var index;
$.each(nodes, function(i,e){
if (e.tagName == 'STRONG') {
index = i;
return false;
}
});
8var index;
$.each(nodes, function(i,e){
if (e.tagName == 'STRONG') {
index = i;
return false;
}
});
3html
var index;
$.each(nodes, function(i,e){
if (e.tagName == 'STRONG') {
index = i;
return false;
}
});
8Output:
Sử dụng thuộc tính TextContent: Chúng tôi cũng có thể sử dụng thuộc tính TextContent để lấy văn bản từ phần tử HTML. We can also use textContent property to get the text from HTML element.
Example:
<!DOCTYPE html>
<
html
var nodes = $('#test')[0].childNodes;
0<
var nodes = $('#test')[0].childNodes;
2var nodes = $('#test')[0].childNodes;
0var nodes = $('#test')[0].childNodes;
4<
var nodes = $('#test')[0].childNodes;
6var nodes = $('#test')[0].childNodes;
0var nodes = $('#test')[0].childNodes;
8var nodes = $('#test')[0].childNodes;
9var nodes = $('#test')[0].childNodes;
8var index;
$.each(nodes, function(i,e){
if (e.tagName == 'STRONG') {
index = i;
return false;
}
});
1var nodes = $('#test')[0].childNodes;
4var index;
$.each(nodes, function(i,e){
if (e.tagName == 'STRONG') {
index = i;
return false;
}
});
3var nodes = $('#test')[0].childNodes;
6var nodes = $('#test')[0].childNodes;
0var index;
$.each(nodes, function(i,e){
if (e.tagName == 'STRONG') {
index = i;
return false;
}
});
3var nodes = $('#test')[0].childNodes;
2var index;
$.each(nodes, function(i,e){
if (e.tagName == 'STRONG') {
index = i;
return false;
}
});
8<
var text = nodes[index + 1].nodeValue;
0var index;
$.each(nodes, function(i,e){
if (e.tagName == 'STRONG') {
index = i;
return false;
}
});
8var nodes = $('#test')[0].childNodes;
4<
var text = nodes[index + 1].nodeValue;
4 var text = nodes[index + 1].nodeValue;
5var text = nodes[index + 1].nodeValue;
6var text = nodes[index + 1].nodeValue;
7var index;
$.each(nodes, function(i,e){
if (e.tagName == 'STRONG') {
index = i;
return false;
}
});
8var nodes = $('#test')[0].childNodes;
8[0]
0var nodes = $('#test')[0].childNodes;
4var index;
$.each(nodes, function(i,e){
if (e.tagName == 'STRONG') {
index = i;
return false;
}
});
3var text = nodes[index + 1].nodeValue;
4var index;
$.each(nodes, function(i,e){
if (e.tagName == 'STRONG') {
index = i;
return false;
}
});
8var nodes = $('#test')[0].childNodes;
4<
[0]
7var index;
$.each(nodes, function(i,e){
if (e.tagName == 'STRONG') {
index = i;
return false;
}
});
8var nodes = $('#test')[0].childNodes;
8childNodes
0var nodes = $('#test')[0].childNodes;
8var nodes = $('#test')[0].childNodes;
08var nodes = $('#test')[0].childNodes;
4var index;
$.each(nodes, function(i,e){
if (e.tagName == 'STRONG') {
index = i;
return false;
}
});
3[0]
7var index;
$.each(nodes, function(i,e){
if (e.tagName == 'STRONG') {
index = i;
return false;
}
});
8var index;
$.each(nodes, function(i,e){
if (e.tagName == 'STRONG') {
index = i;
return false;
}
});
3var text = nodes[index + 1].nodeValue;
0var index;
$.each(nodes, function(i,e){
if (e.tagName == 'STRONG') {
index = i;
return false;
}
});
8var index;
$.each(nodes, function(i,e){
if (e.tagName == 'STRONG') {
index = i;
return false;
}
});
3html
var index;
$.each(nodes, function(i,e){
if (e.tagName == 'STRONG') {
index = i;
return false;
}
});
8Output:
var index;
$.each(nodes, function(i,e){
if (e.tagName == 'STRONG') {
index = i;
return false;
}
});
3html
var index;
$.each(nodes, function(i,e){
if (e.tagName == 'STRONG') {
index = i;
return false;
}
});
8
- Sử dụng thuộc tính TextContent: Chúng tôi cũng có thể sử dụng thuộc tính TextContent để lấy văn bản từ phần tử HTML.human-readable text while the
var nodes = $('#test')[0].childNodes;
20 property returns all the text including var nodes = $('#test')[0].childNodes;
21 and var nodes = $('#test')[0].childNodes;
22 tag. var nodes = $('#test')[0].childNodes;
8var index;
$.each(nodes, function(i,e){
if (e.tagName == 'STRONG') {
index = i;
return false;
}
});
1- Sự khác biệt giữa bên trong và textContent:HTMLElement objects, whereas
var nodes = $('#test')[0].childNodes;
20 property is defined for all Node objects.
Bạn có thể sử dụng $ ('. GetText'). Text ();Trong jQuery.$('. gettext'). text(); in jQuery.
Bạn có thể sử dụng điều này: var ements = document.getEuityById ('txt');var text = Element.innerText ||yếu tố.var element = document. getElementById('txt'); var text = element. innerText || element.