Hướng dẫn remove text from html element - xóa văn bản khỏi phần tử html

Tôi có:

Here is

Text, that I want to

be removed

Tôi muốn:

Cách dễ nhất để loại bỏ tất cả văn bản, nhưng giữ cấu trúc HTML là gì?

3
  • 4
  • 5
  • Đã trả lời ngày 27 tháng 8 năm 2015 lúc 11:17Aug 27, 2015 at 11:17

    6. Và bạn không cần bất kỳ đệ quy nào. jQuery làm điều đó cho bạn:

    $[function[] {
      $["#to-clean, #to-clean *"]                  // selects the element and all element nodes inside it
        .contents[]                                // selects all child nodes including tags, comments and text
        .filter[function[] {
          return this.nodeType === Node.TEXT_NODE; // filter text nodes
        }].remove[];                               // boom!
    }];
    
    
    Here is

    Text, that I want to

    be removed

    Đã trả lời ngày 27 tháng 8 năm 2015 lúc 11:21Aug 27, 2015 at 11:21

    6. Và bạn không cần bất kỳ đệ quy nào. jQuery làm điều đó cho bạn:

    clones=[];
    
    $['div'].children[].each[function[] {
    
    clones.push[$[this].clone[].text['']];
    }];
    
    $['div'].empty[];
    
    for[i=0;i 0]{
           $this.children[].each[RemoveText[$this]];
         }
         else{
           $this.text[""];
         }     
       }];
    }
    

    Đã trả lời ngày 27 tháng 8 năm 2015 lúc 11:14Aug 27, 2015 at 11:16

    Jonathan Dumainejonathan DumaineZoran P.

    5.4053 Huy hiệu vàng36 Huy hiệu bạc 50 Huy hiệu Đồng1 gold badge13 silver badges16 bronze badges

    2

    try:

    function empty[e] {
        var childs = e.children;
        for[var i=0;i

    Chủ Đề