Đ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ó.
Ví dụ HTML:
<div id="test"> <strong>1)</strong> TEXT THAT I ONLY NEED <p>some par</p> <ul> <li>asdf</li> <li>qwerty</li> </ul> </div>Sử dụng jQuery để tìm div, sử dụng [0] để lấy phần tử DOM ra khỏi đối tượng jQuery và thuộc tính childNodes để có được con cái của nó:
var nodes = $('#test')[0].childNodes;Sau đó, bạn có thể sử dụng jQuery để lặp qua các nút để tìm nút là phần tử strong:
var index; $.each(nodes, function(i,e){ if (e.tagName == 'STRONG') { index = i; return false; } });Bây giờ bạn có thể sử dụng DOM để lấy giá trị văn bản từ nút tiếp theo:
var text = nodes[index + 1].nodeValue;Cải thiện bài viết
Lưu bài viết
Cả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>
<htmlvar nodes = $('#test')[0].childNodes; 0
<var nodes = $('#test')[0].childNodes; 2var nodes = $('#test')[0].childNodes; 0
var nodes = $('#test')[0].childNodes; 4<var nodes = $('#test')[0].childNodes; 6var nodes = $('#test')[0].childNodes; 0
var nodes = $('#test')[0].childNodes; 8var nodes = $('#test')[0].childNodes; 9
var nodes = $('#test')[0].childNodes; 8var index; $.each(nodes, function(i,e){ if (e.tagName == 'STRONG') { index = i; return false; } }); 1
var 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; 0
var 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; } }); 8
var 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; } }); 8
var nodes = $('#test')[0].childNodes; 8[0]0
var 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; } }); 8
var nodes = $('#test')[0].childNodes; 4<[0]7var index; $.each(nodes, function(i,e){ if (e.tagName == 'STRONG') { index = i; return false; } }); 8
var nodes = $('#test')[0].childNodes; 8childNodes0
var nodes = $('#test')[0].childNodes; 8childNodes2
var 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; } }); 8
var 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; } }); 8
var index; $.each(nodes, function(i,e){ if (e.tagName == 'STRONG') { index = i; return false; } }); 3htmlvar index; $.each(nodes, function(i,e){ if (e.tagName == 'STRONG') { index = i; return false; } }); 8
Output:
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>
<htmlvar nodes = $('#test')[0].childNodes; 0
<var nodes = $('#test')[0].childNodes; 2var nodes = $('#test')[0].childNodes; 0
var nodes = $('#test')[0].childNodes; 4<var nodes = $('#test')[0].childNodes; 6var nodes = $('#test')[0].childNodes; 0
var nodes = $('#test')[0].childNodes; 8var nodes = $('#test')[0].childNodes; 9
var nodes = $('#test')[0].childNodes; 8var index; $.each(nodes, function(i,e){ if (e.tagName == 'STRONG') { index = i; return false; } }); 1
var 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; 0
var 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; } }); 8
var 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; } }); 8
var nodes = $('#test')[0].childNodes; 8[0]0
var 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; } }); 8
var nodes = $('#test')[0].childNodes; 4<[0]7var index; $.each(nodes, function(i,e){ if (e.tagName == 'STRONG') { index = i; return false; } }); 8
var nodes = $('#test')[0].childNodes; 8childNodes0
var nodes = $('#test')[0].childNodes; 8var nodes = $('#test')[0].childNodes; 08
var 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; } }); 8
var 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; } }); 8
var index; $.each(nodes, function(i,e){ if (e.tagName == 'STRONG') { index = i; return false; } }); 3htmlvar index; $.each(nodes, function(i,e){ if (e.tagName == 'STRONG') { index = i; return false; } }); 8
Output:
var index; $.each(nodes, function(i,e){ if (e.tagName == 'STRONG') { index = i; return false; } }); 3htmlvar 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.