Hướng dẫn how to remove html tags from string jquery? - làm thế nào để loại bỏ các thẻ html khỏi chuỗi jquery?

Nếu bạn cần xóa HTML nhưng không biết liệu nó có thực sự chứa bất kỳ thẻ HTML nào hay không, bạn không thể sử dụng phương thức jQuery trực tiếp vì nó trả về trình bao bọc trống cho văn bản không HTML.

$('<div>Hello world</div>').text(); //returns "Hello world"
$('Hello world').text(); //returns empty string ""

Bạn phải bọc văn bản trong HTML hợp lệ:

$('<div>' + 'Hello world' + '</div>').text();

Hoặc sử dụng phương thức $ .ParseHtml () (vì jQuery 1.8) có thể xử lý cả văn bản HTML và không HTML:

var html = $.parseHTML('Hello world'); //parseHTML return HTMLCollection
var text = $(html).text(); //use $() to get .text() method

Cộng với parsehtml loại bỏ hoàn toàn các thẻ tập lệnh hữu ích như bảo vệ chống hack cho đầu vào của người dùng.

$('<p>Hello world</p><script>console.log(document.cookie)</script>').text();
//returns "Hello worldconsole.log(document.cookie)"

$($.parseHTML('<p>Hello world</p><script>console.log(document.cookie)</script>')).text();
//returns "Hello world"

Hướng dẫn how to remove html tags from string jquery? - làm thế nào để loại bỏ các thẻ html khỏi chuỗi jquery?

& nbsp; Sonika vào ngày 07 tháng 12 năm 2012 03:18 AM 24443 & nbsp; Lượt xemSonika
on Dec 07, 2012 03:18 AM
24443 Views

Eiter tôi đã đặt câu hỏi là

Cách xóa thẻ HTML khỏi chuỗi bằng jQuery.

Ví dụ: & nbsp; dữ liệu & nbsp;

Dữ liệu sẽ được trả về sau khi xóa HTML. Điều này có thể được thực hiện bởi

Ngoài ra nếu người dùng nhập HTML không hợp lệ như & NBSP; dữ liệu

(Ở đây không có thẻ đóng nào được đưa ra thì cũng nên lấy lại dữ liệu)

& nbsp; và tôi đã nhận được trả lời bằng cách làm

function

functio RemoveHTML(html)
{
return $("<div />").html(html).text();
}

Nhưng điều này không hoạt động. Khi tôi chỉ cung cấp thẻ đóng dưới dạng đầu vào như nó không loại bỏ HTML này.

Xin đề nghị.

Tải xuống API miễn phí cho Word, Excel và PDF trong ASP.NET: Tải xuống

Hướng dẫn how to remove html tags from string jquery? - làm thế nào để loại bỏ các thẻ html khỏi chuỗi jquery?

Với biểu thức chính quy, bạn có thể làm điều đó

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript">
        function RemoveHTMLTags() {
            var regX = /(<([^>]+)>)/ig;
            var html = document.getElementById("txtHTML").value;
            alert(html.replace(regX, ""));
        }
    </script>
</head>
<body>
    <input type = "text" id = "txtHTML" value = "How are </table> you?" />
    <input type="button" id="btn" onclick="RemoveHTMLTags()" value="Remove HTML Tags" />
</body>
</html>

Thử nghiệm


Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọcreplace() function and can also use .textContent property, .innerText property from HTML DOM. HTML tags are of two types opening tag and closing tag.

    • Bàn luận It starts with a ‘<‘, followed by an HTML keyword and ends with a ‘>‘. ,
      , are some examples of HTML opening tags.</li><li><strong>Để loại bỏ tất cả các thẻ HTML từ một chuỗi, có rất nhiều thủ tục trong JavaScript. Để loại bỏ các thẻ, chúng tôi có thể sử dụng hàm thay thế () và cũng có thể sử dụng thuộc tính .textContent, .innertext thuộc tính từ HTML DOM. Thẻ HTML có hai loại thẻ mở và thẻ đóng.</strong> It starts with a ‘<strong></</strong>‘, followed by an HTML keyword and ends with a ‘<strong>></strong>‘.</html>, are examples of HTML closing tags.

    Tag mở đầu: Nó bắt đầu bằng một ‘. ,, là một số ví dụ về các thẻ mở HTML.

    Thẻ đóng: Nó bắt đầu bằng một ‘., Là các ví dụ về các thẻ đóng HTML. The ‘<‘, ‘’, can be used to identify a word as an HTML tag in a string. The following examples show how to strip out HTML tags using replace() function and a regular expression, which identifies an HTML tag in the input string. A regular expression is a better way to find the HTML tags and remove them easily.

    • Các ví dụ dưới đây minh họa cả hai phương pháp: In JavaScript, the following code strips a string of the HTML tags.

      <script>

      Ví dụ 1: The ‘

      Chương trình: Trong JavaScript, mã sau đây một chuỗi các thẻ HTML.

      function

      $('<div>' + 'Hello world' + '</div>').text();
      
      0

      $('<div>' + 'Hello world' + '</div>').text();
      
      1
      var html = $.parseHTML('Hello world'); //parseHTML return HTMLCollection
      var text = $(html).text(); //use $() to get .text() method
      
      3

      $('<div>' + 'Hello world' + '</div>').text();
      
      8
      var html = $.parseHTML('Hello world'); //parseHTML return HTMLCollection
      var text = $(html).text(); //use $() to get .text() method
      
      5

      $('<div>' + 'Hello world' + '</div>').text();
      
      1
      $('<div>' + 'Hello world' + '</div>').text();
      
      2
      $('<div>' + 'Hello world' + '</div>').text();
      
      3
      $('<div>' + 'Hello world' + '</div>').text();
      
      4
      $('<div>' + 'Hello world' + '</div>').text();
      
      5
      $('<div>' + 'Hello world' + '</div>').text();
      
      6
      $('<div>' + 'Hello world' + '</div>').text();
      
      7

      $('<p>Hello world</p><script>console.log(document.cookie)</script>').text();
      //returns "Hello worldconsole.log(document.cookie)"
      
      $($.parseHTML('<p>Hello world</p><script>console.log(document.cookie)</script>')).text();
      //returns "Hello world"
      
      1

      $('<p>Hello world</p><script>console.log(document.cookie)</script>').text();
      //returns "Hello worldconsole.log(document.cookie)"
      
      $($.parseHTML('<p>Hello world</p><script>console.log(document.cookie)</script>')).text();
      //returns "Hello world"
      
      2

      $('<div>' + 'Hello world' + '</div>').text();
      
      1
      $('<p>Hello world</p><script>console.log(document.cookie)</script>').text();
      //returns "Hello worldconsole.log(document.cookie)"
      
      $($.parseHTML('<p>Hello world</p><script>console.log(document.cookie)</script>')).text();
      //returns "Hello world"
      
      4
      $('<p>Hello world</p><script>console.log(document.cookie)</script>').text();
      //returns "Hello worldconsole.log(document.cookie)"
      
      $($.parseHTML('<p>Hello world</p><script>console.log(document.cookie)</script>')).text();
      //returns "Hello world"
      
      5

      $('<p>Hello world</p><script>console.log(document.cookie)</script>').text();
      //returns "Hello worldconsole.log(document.cookie)"
      
      $($.parseHTML('<p>Hello world</p><script>console.log(document.cookie)</script>')).text();
      //returns "Hello world"
      
      6

    • Output:
      Welcome to GeeksforGeeks.
      Welcome to GeeksforGeeks.

    $('<div>' + 'Hello world' + '</div>').text();
    
    8
    $('<div>' + 'Hello world' + '</div>').text();
    
    9
    var html = $.parseHTML('Hello world'); //parseHTML return HTMLCollection
    var text = $(html).text(); //use $() to get .text() method
    
    0
    var html = $.parseHTML('Hello world'); //parseHTML return HTMLCollection
    var text = $(html).text(); //use $() to get .text() method
    
    1
    The .textContent property returns the text content of the specified node and all its descendants. The .innerText property do the same thing as .textContent property.

    • Các ví dụ dưới đây minh họa cả hai phương pháp: In JavaScript, the following code strips a string of the HTML tags.

      <script>

      Ví dụ 1: The ‘

      functio RemoveHTML(html)
      {
      return $("<div />").html(html).text();
      }
      2
      functio RemoveHTML(html)
      {
      return $("<div />").html(html).text();
      }
      3
      functio RemoveHTML(html)
      {
      return $("<div />").html(html).text();
      }
      4
      var html = $.parseHTML('Hello world'); //parseHTML return HTMLCollection
      var text = $(html).text(); //use $() to get .text() method
      
      1

      Chương trình: Trong JavaScript, mã sau đây một chuỗi các thẻ HTML.

      function

      $('<div>' + 'Hello world' + '</div>').text();
      
      0

      $('<div>' + 'Hello world' + '</div>').text();
      
      1
      $('<div>' + 'Hello world' + '</div>').text();
      
      2
      $('<div>' + 'Hello world' + '</div>').text();
      
      3
      $('<div>' + 'Hello world' + '</div>').text();
      
      4
      $('<div>' + 'Hello world' + '</div>').text();
      
      5
      $('<div>' + 'Hello world' + '</div>').text();
      
      6
      $('<div>' + 'Hello world' + '</div>').text();
      
      7

      $('<div>' + 'Hello world' + '</div>').text();
      
      1
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
          <title></title>
          <script type="text/javascript">
              function RemoveHTMLTags() {
                  var regX = /(<([^>]+)>)/ig;
                  var html = document.getElementById("txtHTML").value;
                  alert(html.replace(regX, ""));
              }
          </script>
      </head>
      <body>
          <input type = "text" id = "txtHTML" value = "How are </table> you?" />
          <input type="button" id="btn" onclick="RemoveHTMLTags()" value="Remove HTML Tags" />
      </body>
      </html>
      9

      Welcome to GeeksforGeeks.
      0

    • Output:
      A Computer Science Portal for Geeks
      A Computer Science Portal for Geeks

    Làm thế nào để xóa tất cả các thẻ HTML khỏi một chuỗi?

    Các thẻ HTML có thể được xóa khỏi một chuỗi đã cho bằng cách sử dụng phương thức thay thế () của lớp chuỗi. Chúng ta có thể xóa các thẻ HTML khỏi một chuỗi đã cho bằng cách sử dụng biểu thức thông thường. Sau khi xóa các thẻ HTML khỏi một chuỗi, nó sẽ trả về một chuỗi dưới dạng văn bản thông thường.by using replaceAll() method of String class. We can remove the HTML tags from a given string by using a regular expression. After removing the HTML tags from a string, it will return a string as normal text.

    Làm thế nào để xóa tất cả các thẻ HTML khỏi chuỗi JS?

    Để loại bỏ tất cả các thẻ HTML từ một chuỗi, có rất nhiều thủ tục trong JavaScript. Để loại bỏ các thẻ, chúng tôi có thể sử dụng hàm thay thế () và cũng có thể sử dụng thuộc tính .textContent, .innertext thuộc tính từ HTML DOM.use replace() function and can also use . textContent property, . innerText property from HTML DOM.

    Làm thế nào để bạn xóa HTML khỏi văn bản?

    Xóa thẻ HTML khỏi văn bản..
    Nhấn Ctrl+H.....
    Nhấp vào nút Thêm, nếu nó có sẵn.....
    Đảm bảo hộp kiểm sử dụng WildCards được chọn ..
    Trong tìm hộp nào, nhập như sau: \ ([!
    Trong hộp thay thế, nhập như sau: \ 1 ..
    Với điểm chèn vẫn còn trong hộp thay thế, nhấn Ctrl+I một lần ..

    Làm thế nào loại bỏ thẻ HTML khỏi chuỗi trong React?

    // Xóa thẻ HTML khỏi chuỗi, chỉ để lại hàm văn bản bên trong RemoveHtml (str) {var tmp = document.createelement ("div");tmp.innerhtml = str;Trả về TMP.TextContent ||TMP.removeHTML(str){ var tmp = document. createElement("DIV"); tmp. innerHTML = str; return tmp. textContent || tmp.