jQuery 関数では、同じクラスを持ついくつかの HTML 要素をループ処理し、特定の文字列を各反復でそれらの要素のコンテンツと比較する必要があります。
誰かがこれを行う方法のヒントを教えてもらえますか?
jQuery 関数では、同じクラスを持ついくつかの HTML 要素をループ処理し、特定の文字列を各反復でそれらの要素のコンテンツと比較する必要があります。
誰かがこれを行う方法のヒントを教えてもらえますか?
ここにJS FIDDLEのデモがあります。チェックしてください。
HTML :
<div class="my_class">This is div 1</div>
<div class="my_class">This is div 2</div>
<div class="my_class">This is div 3</div>
<div class="my_class">This is div 4</div>
JS:
var search_text = "This is div 4";
// Loop through each item
$('.my_class').each(function(index) {
var current_elem_text = $(this).html();
alert(current_elem_text);
if(current_elem_text == search_text)
{
alert('Hey ! it matches ');
}
else
{
alert('Sorry it does not match ');
}
});