2011-11-11 9 views
1

削除または削除したいテキストが繰り返し含まれているアイテムが多数あります。だから、これから:jQueryを使ってアイテムリストの繰り返し単語を削除する

<ul class="juicy"> 
<li>Juicy Green Apples</li> 
<li>Juicy Green Tomatoes</li> 
<li>Juicy Green Broccoli</li> 
</ul> 

私はこれを達成したいと思います:あなたが交換されているテキストが事前にわかっている場合

<ul class="juicy"> 
<li>Apples</li> 
<li>Tomatoes</li> 
<li>Broccoli</li> 
</ul> 
+2

は、それが常に同じテキストですか?あるいは、テキスト内の重複した単語の一般的な解決策の後にいますか? –

+0

はい、手前の言葉がわかります。実際、削除するには他の文字列があるかもしれません。 – Seb

答えて

1

jQueryの.text()方法はかなり簡単にこれを扱うことができます。

var textToReplace = "Juicy Green", 
    re = new RegExp(textToReplace,"i"); 
$(".juicy li").text(function(index,text){ 
    return text.replace(re,''); 
}); 

編集:コメントでの質問への答え:このような

何か:

var textToReplaceArr = ["Juicy Green","Sour Yellow"]; 
for (var i = 0; i < textToReplaceArr.length; i++) { 
    $(".juicy li").text(function(index,text){ 
    var re = new RegExp(textToReplaceArr[i],"i"); 
    return text.replace(re,''); 
    }); 
} 
+0

ありがとうKevin。これはうまく動作します。言葉は確かに事前に知られています。 削除したい同じULに別の単語が含まれている場合はどうなりますか?私は単純にコードを複製し、キーワードを別のセットに置き換えてみました。よりエレガントで簡潔な方法があるのだろうか? – Seb

+0

素晴らしい、ありがとう。あなたは雇われている;) – Seb

0

あなたは、動的な何かを試してみたいですか?

$(document).ready(

function() { 
    var repeatedWordsArray = new Array(); 
    var wordsToRemoveArray = new Array(); 
    var i = 0; 
    $($("ul.juicy > li").map(function() { 
     return $(this).text(); 
    }).get().join(" ").split(/\s+/g)).each( //Joins the text of all elements, appends space between them and then splits with space character 

    function() { 
     repeatedWordsArray[this] = repeatedWordsArray[this] == undefined ? 1 : repeatedWordsArray[this] + 1;  //Increments the counter when the same word is encountered 
     if (repeatedWordsArray[this] == 2) {  //If found twice, make a note of the word 
      wordsToRemoveArray[i++] = this; 
     } 
    }); 
    if (wordsToRemoveArray.length > 0) { 
     $("ul.juicy > li").each(
     function() { 
      var ulElement = this; 
      $(wordsToRemoveArray).each(
       function() { 
        var regexp = new RegExp('^\\s*\\w+\\s*$'); 
        if(!$(ulElement).text().match(regexp)) {  //Do not match if the text of element contains a single word with or without spaces at its ends 
         regexp = new RegExp('\\s*' + this + '\\s*','g'); 
         $(ulElement).text($(ulElement).text().replace(regexp, '')); //Set the text of the element after removing the repeatedly found word 
        } 
       } 
       ); 
      } 
     ); 
    } 
} 
); 

これは、あまりにも、次のULのために働く:

<ul class="juicy"> 
    <li>Juicy Green Apples</li> 
    <li>Juicy Green Tomatoes</li> 
    <li>Juicy Green Broccoli</li> 
    <li>Juicy</li> 
    <li>  Green </li> 
</ul> 
+0

ジェームス、それはかなり素晴らしいです。ありがとうございました。ただし、コードを使用する場合は、削除するキーワードをどこに挿入すればよいですか? – Seb

+0

これを実装するには、キーワードを追加する必要はありません。この操作が行われるべきul項目のクラス名を指定するだけで済みます。ここで$( "ul.juicy> li")は、どのアイテムを適用するかを決定します。 'ul.juicy> li'は、名前が 'juicy'のクラスを持つ 'ul'親要素の中の即時の 'li'項目を意味します。これはコードで2回発生します。これをjQueryセレクタに置き換えると、すべて正常に動作するはずです。 –

+0

ああ、そうです。それは自動的にうまく動作し、ulの繰り返しキーワードはすべて削除されます。ありがとう。非常にきれい。 – Seb