2017-09-02 18 views
0

属性を持たない<span>を返すjQueryコードを書きましたが、問題は数字と英語のテキストに対して2回の警告です。特定の属性を持つタグを選択してください

はJavaScript

$(document).ready(function() { 
 
    var text = $("span:not([dir=rtl])").text(); 
 
    alert(text); 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="content"> 
 
    <h2>Blessing upon the Bearers of the Throne</h2> 
 
    <button id="btn">Click</button> 
 
    <p style="text-align:center">&nbsp; </p> 
 
    <p style="text-align:center"> 
 
    <span style="font-size11pt"> 
 
     <span style="font-family:Calibri,sans-serif">O God, as for the Bearers of Thy Throne, who never flag in glorifying Thee, </span> 
 
    </span> 
 
    </p> 
 
    <p style="text-align:center"> 
 
     <span style="font-size11pt"> 
 
      <span style="font-family:Calibri,sans-serif"> 
 
       <span dir="rtl" lang="AR-SA" style="font-family&quot;Arial&quot;,&quot;sans-serif&quot;">111111111111111111111111111َ </span> 
 
      </span> 
 
     </span> 
 
    </p> 
 
</div>

+0

おそらくあなたは、その属性を持たない唯一の最も内側のスパンを選択したいのですが、あなたの現在のセレクタは、親のスパンを選択します。 –

+0

@IlyaStreltsyn does not workより明確で正しいコードを書くことができますか? – sepehr

+2

ここにはjsfiddle:https: //jsfiddle.net/cb69m3e9/(タブレットから入力された以前の誤字はごめん。それは自動修正がそれを台無しにしたようだ) –

答えて

0

のjQueryで使用attr。これは動作します

次の例を確認することができます。

$(document).ready(function(){ 
 
    $("span").each(function(){ 
 
    \t \t if($(this).attr('dir') == 'ltr'){ 
 
     \t alert($(this).text()); 
 
     } 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<span dir="rtl">Coffee</span> 
 
<span dir="ltr">Milk</span> 
 
<span dir="rtl">Soda</span>

関連する問題