2011-10-15 3 views
0

나는 (태그 <p>) 그들을 그것들을 비슷하게 비교해보고 그것들을 제거하고, 비슷하지 않은 다른 단어를 보여주고 싶다. 하지만이 코드가 제거 된 how are you? beacuse가 비슷하다고 생각합니다 how, 어떻게해야합니까?jQuery와 함께 문구 비교

예 : 케이스 출력http://jsfiddle.net/CCMKu/

<span style="display: none;"> 
    <p>hello</p> 
    <p>hi</p> 
    <p>how</p> 
    <p>what</p> 
</span> 

<div class="remove"> 
    <p>hello</p>  
    <p>how</p>  
    <p>how are you?</p> 
    <p>what</p> 
    <p>fine</p> 
    <p>hi</p> 
</div> 



$("span p").each(function() { 
    $(".remove p:contains(" + $(this).text() + ")").remove(); 
}); 

출력된다 finefine $ how are you?이어야한다.

답변

1

시도 :

$("span p").each(function() { 
    var item=$(this).text(); 
    $(".remove p").filter(function(){return $(this).text()==item}).remove(); 
}); 
1

당신을 위해 바이올린을 업데이트 : http://jsfiddle.net/CCMKu/3/

var input = []; 

/* 
* Storing all values 
* in a single array for 
* a faster access 
*/ 
$("span p").each(function() { 
    input.push($(this).text()); 
}); 

$(".remove p").each(function() { 
    var $this = $(this); 

    /* 
    * Now use the inArray utility 
    * function to find duplicates 
    * 
    * EDIT: Added toLowerCase() to allow 
    * case-insensitive matching 
    */ 
    if ($.inArray($this.text().toLowerCase(), input.toLowerCase()) !== -1) { 
     $this.remove(); 
    } 
});