Hướng dẫn jquery get only text without html - jquery chỉ nhận được văn bản mà không có html

Đ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
  • Bàn luận
  • Cải thiện bài viết

    Lưu bài viết

    Đọc

    1. Bàn luận
    2. 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;
    
    2
    var nodes = $('#test')[0].childNodes;
    
    0

    var nodes = $('#test')[0].childNodes;
    
    4<
    var nodes = $('#test')[0].childNodes;
    
    6
    var nodes = $('#test')[0].childNodes;
    
    0

    var nodes = $('#test')[0].childNodes;
    
    8
    var nodes = $('#test')[0].childNodes;
    
    9

    var nodes = $('#test')[0].childNodes;
    
    8
    var index;
    $.each(nodes, function(i,e){
      if (e.tagName == 'STRONG') {
        index = i;
        return false;
      }
    });
    
    1

    var nodes = $('#test')[0].childNodes;
    
    4
    var index;
    $.each(nodes, function(i,e){
      if (e.tagName == 'STRONG') {
        index = i;
        return false;
      }
    });
    
    3
    var nodes = $('#test')[0].childNodes;
    
    6
    var nodes = $('#test')[0].childNodes;
    
    0

    var index;
    $.each(nodes, function(i,e){
      if (e.tagName == 'STRONG') {
        index = i;
        return false;
      }
    });
    
    3
    var nodes = $('#test')[0].childNodes;
    
    2
    var index;
    $.each(nodes, function(i,e){
      if (e.tagName == 'STRONG') {
        index = i;
        return false;
      }
    });
    
    8

    <

    var text = nodes[index + 1].nodeValue;
    
    0
    var 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;
    
    5
    var text = nodes[index + 1].nodeValue;
    
    6
    var text = nodes[index + 1].nodeValue;
    
    7
    var 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;
    
    4
    var index;
    $.each(nodes, function(i,e){
      if (e.tagName == 'STRONG') {
        index = i;
        return false;
      }
    });
    
    3
    var text = nodes[index + 1].nodeValue;
    
    4
    var index;
    $.each(nodes, function(i,e){
      if (e.tagName == 'STRONG') {
        index = i;
        return false;
      }
    });
    
    8

    var nodes = $('#test')[0].childNodes;
    
    4<[0]7
    var 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;
    
    4
    var index;
    $.each(nodes, function(i,e){
      if (e.tagName == 'STRONG') {
        index = i;
        return false;
      }
    });
    
    3[0]7
    var 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;
      }
    });
    
    3
    var text = nodes[index + 1].nodeValue;
    
    0
    var 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;
      }
    });
    
    3html
    var index;
    $.each(nodes, function(i,e){
      if (e.tagName == 'STRONG') {
        index = i;
        return false;
      }
    });
    
    8

    Output:

    Hướng dẫn jquery get only text without html - jquery chỉ nhận được văn bản mà không có html

    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;
    
    2
    var nodes = $('#test')[0].childNodes;
    
    0

    var nodes = $('#test')[0].childNodes;
    
    4<
    var nodes = $('#test')[0].childNodes;
    
    6
    var nodes = $('#test')[0].childNodes;
    
    0

    var nodes = $('#test')[0].childNodes;
    
    8
    var nodes = $('#test')[0].childNodes;
    
    9

    var nodes = $('#test')[0].childNodes;
    
    8
    var index;
    $.each(nodes, function(i,e){
      if (e.tagName == 'STRONG') {
        index = i;
        return false;
      }
    });
    
    1

    var nodes = $('#test')[0].childNodes;
    
    4
    var index;
    $.each(nodes, function(i,e){
      if (e.tagName == 'STRONG') {
        index = i;
        return false;
      }
    });
    
    3
    var nodes = $('#test')[0].childNodes;
    
    6
    var nodes = $('#test')[0].childNodes;
    
    0

    var index;
    $.each(nodes, function(i,e){
      if (e.tagName == 'STRONG') {
        index = i;
        return false;
      }
    });
    
    3
    var nodes = $('#test')[0].childNodes;
    
    2
    var index;
    $.each(nodes, function(i,e){
      if (e.tagName == 'STRONG') {
        index = i;
        return false;
      }
    });
    
    8

    <

    var text = nodes[index + 1].nodeValue;
    
    0
    var 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;
    
    5
    var text = nodes[index + 1].nodeValue;
    
    6
    var text = nodes[index + 1].nodeValue;
    
    7
    var 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;
    
    4
    var index;
    $.each(nodes, function(i,e){
      if (e.tagName == 'STRONG') {
        index = i;
        return false;
      }
    });
    
    3
    var text = nodes[index + 1].nodeValue;
    
    4
    var index;
    $.each(nodes, function(i,e){
      if (e.tagName == 'STRONG') {
        index = i;
        return false;
      }
    });
    
    8

    var nodes = $('#test')[0].childNodes;
    
    4<[0]7
    var 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;
    
    8
    var nodes = $('#test')[0].childNodes;
    
    08

    var nodes = $('#test')[0].childNodes;
    
    4
    var index;
    $.each(nodes, function(i,e){
      if (e.tagName == 'STRONG') {
        index = i;
        return false;
      }
    });
    
    3[0]7
    var 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;
      }
    });
    
    3
    var text = nodes[index + 1].nodeValue;
    
    0
    var 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;
      }
    });
    
    3html
    var index;
    $.each(nodes, function(i,e){
      if (e.tagName == 'STRONG') {
        index = i;
        return false;
      }
    });
    
    8

    Output:

    Hướng dẫn jquery get only text without html - jquery chỉ nhận được văn bản mà không có html

    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

    1. 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.
    2. var nodes = $('#test')[0].childNodes;
      
      8
      var index;
      $.each(nodes, function(i,e){
        if (e.tagName == 'STRONG') {
          index = i;
          return false;
        }
      });
      
      1
    3. 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.

    Thuộc tính var nodes = $('#test')[0].childNodes; 19 chỉ trả về văn bản có thể đọc được của con người trong khi thuộc tính var nodes = $('#test')[0].childNodes; 20 trả về tất cả các văn bản bao gồm thẻ var nodes = $('#test')[0].childNodes; 21 và var nodes = $('#test')[0].childNodes; 22.

    Sử dụng thuộc tính InnerText: Chúng ta có thể sử dụng thuộc tính bên trong để lấy văn bản từ phần tử HTML.Đầu ra: Sử dụng thuộc tính TextContent: Chúng ta 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 use innerText property to get the text from HTML element. Output: Using textContent property: We can also use textContent property to get the text from HTML element.

    Làm thế nào để có được một văn bản mà không có thẻ trong jQuery?

    Bạn có thể sử dụng $ ('. GetText'). Text ();Trong jQuery.$('. gettext'). text(); in jQuery.

    Làm thế nào để hiển thị văn bản mà không có thẻ HTML trong JavaScript?

    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.