2011-10-15 84 views
0

我想将它们(与标记<p>)进行比较,如果它们相似,请将它们移除,并显示其他不相似的单词。但此代码删除how are you?因为它认为它是类似的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(); 
}); 

输出是fine万一输出应该fine $ 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(); 
    } 
});