복붙노트

[JQUERY] jQuery : 아이들을 extrude .text () [복제]

JQUERY

jQuery : 아이들을 extrude .text () [복제]

해결법


  1. 1.마이크로 플러그인 :

    마이크로 플러그인 :

    $.fn.ignore = function(sel){
      return this.clone().find(sel||">*").remove().end();
    };
    

    ...이 HTML을 가지고 :

    <div id="test"><b>Hello</b><span> World</span>!!!</div>
    

    결과 :

    var text = $('#test').ignore("span").text(); // "Hello!!!"
    var html = $('#test').ignore("span").html(); // "<b>Hello</b>!!!"
    

    당신이 그것을 더 빨리 원하면 즉각적인 아이들을 제외 할 필요가 있으면 ....children (그 대신,


  2. 2.http://jsfiddle.net/r8kNL/

    http://jsfiddle.net/r8kNL/

    $(this).contents().filter(function() {
      return this.nodeType == 3;
    }).text()
    

  3. 3.

    $(this).clone().find('span').remove().end().text();
    

    그렇지 않으면 다른 접근 방식으로 스팬 요소가 끝에 항상 "재생"이라는 단어가 포함되어있는 경우 바꾸기 () 또는 substring () 함수를 사용하십시오.

    var text = $(this).text();
    text = text.substring(0, text.length-4);
    

    후자의 예는 너무 현지화되어 있고 방탄 방지 방법이 아닙니다. 그러나 다른 관점에서 솔루션을 제안하는 것만으로 언급했습니다.


  4. 4.

    $( this.childNodes ).map( function(){
        return this.nodeType === 3 && this.nodeValue || "";
    }).get().join("");
    
  5. from https://stackoverflow.com/questions/11347779/jquery-exclude-children-from-text by cc-by-sa and MIT license