2017-08-08 7 views
1

このウェブサイトの一部の人々の助けを借りて、私はここでハッシュタグカウンタを作成することができました - https://codepen.io/timothycdavis17/pen/ZJeVBjテキストエリアで重複するテキストを削除します

ここにコードがあります。

HTML

<section class="section-textarea bg-primary"> 

    <div class="container"> 
    <div class="row"> 
     <div class="col-lg-12"> 
     <div class="well textarea-well clearfix"> 
      <textarea rows="16" class="form-control"></textarea> 

      <div class="buttons"> 
      <div class="btn-group" role="group" aria-label="Basic example"> 

      <button type="button" class="btn btn-secondary btn-primary btn-lg 
      count-button">Count</button> 

      <button type="button" class="btn btn-secondary btn-danger btn-lg 
      reset-button">Reset</button> 

      </div> 

     </div> 

      <div class="remaining-counter">Hashtags Counted:&nbsp;&nbsp;<span 
      class="well text-well">0</span></div> 


    </div> 

</div> 

</div> 

</div> 

</section> 

CSS

body { 
    background: #0275D8; 
} 

.textarea-well { 
    color: #000; 
    text-align: center; 
    margin: 10% auto; 
} 

textarea { 

    margin-bottom: 20px; 

} 

.text-well { 
    background: lightblue; 
} 

.count-button { 
    margin-bottom: 20px; 
} 

.remaining-counter { 
    font-size: 30px; 
    margin-bottom: 20px; 
} 

jQueryのそれは私がwを頼まれたなされた後

$(".count-button").click(function() { 
    var text = $("textarea").val(); 
    var count = (text.match(/(^|\W)(#[a-z\d A-Z\D][\w-]*)/g) || []).length; 
    $('.text-well').html(count); 

    var seen = {}; 
    $('textarea').each(function() { 
    var txt = $(this).text(); 
    if (seen[txt]) 
     $(this).remove(); 
    else 
     seen[txt] = true; 
    }); 

}).trigger('click'); 


$(".reset-button").click(function() { 
$("textarea").val(' '); 
$('.text-well').html(0); 
}).trigger('click'); 

テキストエリアに入力された重複するハッシュタグをカウントせず、赤い波打ち線でアンダーラインテーマをエラーする可能性があります。

私はこのウェブサイトの周りを見ていましたが、答えが見つからないようでした。

何か助けていただければ幸いです。

答えて

1

重複を除外するために一時配列を使用するだけです。

// save the matches instead of immediately counting 
var tags = text.match(/(^|\W)(#[a-z\d A-Z\D][\w-]*)/g) || []; 
// init empty array for holding unique matches 
var uTags = []; 
// loop through matches 
$.each(tags, function(index, value){ 
    // if match is not in holding array array, add it to holding array 
    if(uTags.indexOf(value) == -1) uTags.push(value); 
}); 
// get count from holding array which will only have one copy of each match 
var count = uTags.length; 

Here is a fork of your codepen with the above code.

+0

おかげで非常に多く、それは重複だユーザーを表示するには、重複するテキストのスタイルを設定する方法はありますか? –

+0

もちろん、if文にelseブロックを追加し、duplicate-foundに実行したいコードを追加してください。 – amflare

+0

私はelse文を追加しようとしましたが、私が正直であることを完全には確信していません。私は私のコードを更新しました –