2017-09-02 125 views
0

我想提醒英文文本,而不是數字,並寫了jQuery代碼返回<span> s沒有rtl屬性,但我的問題是它警告兩次數字和英文文本。選擇文本一個具有特定屬性的標記

的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

也許你只需要選擇最裏面的跨度沒有這個屬性,但當前的選擇也選擇父跨度。你有沒有嘗試類似'span:not([dir =「rtl」]):not(:has(span)'? –

+0

@IlyaStreltsyn does not work can you write more clear and correct code? – sepehr

+2

這是一個jsfiddle:https: //jsfiddle.net/cb69m3e9/(抱歉以前的錯字,從平板電腦中鍵入,似乎自動糾錯會破壞它) –

回答

0

使用attr jQuery中。這將工作

你可以檢查下面的例子。

$(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>

相關問題