从jQuery

时间:2017-09-13 20:50:51

标签: javascript jquery html

我在网上搜索但我找不到正确的答案。 我使用下面的代码将英文数字更改为波斯语。 但代码改变了整个身体的数字,我需要apikey td被排除,任何想法?



$(window).load(function() {
  $("[lang='fa']").find("*").andSelf().contents().each(function() {
    if (this.nodeType === 3) {
      this.nodeValue = this.nodeValue.replace(/\d/g, function(v) {
        return String.fromCharCode(v.charCodeAt(0) + 0x06C0);
      });
    }
  });
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
  <div lang="fa">

    <div>some text here and 1234567890</div>

    <table>
      <tr>
        <td id="apikey">123qwe456qwe123</td>
        <td id="apikey">456ert456</td>
      </tr>

    </table>


  </div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

您的ID必须是唯一的。

使用课程和.not:not

&#13;
&#13;
$(window).load(function() {
  $("[lang='fa']").find("*").andSelf().not(".apikey").contents().each(function() {
    if (this.nodeType === 3) {
      this.nodeValue = this.nodeValue.replace(/\d/g, function(v) {
        return String.fromCharCode(v.charCodeAt(0) + 0x06C0);
      });
    }
  });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
  <div lang="fa">

    <div>some text here and 1234567890</div>

    <table>
      <tr>
        <td class="apikey">123qwe456qwe123</td>
        <td class="apikey">456ert456</td>
      </tr>

    </table>


  </div>
&#13;
&#13;
&#13;

答案 1 :(得分:-1)

只更改您想要更改的内容。给像我给div的changeLanguage类元素。将此类提供给您想要更改的任何元素。

&#13;
&#13;
</script>
<script type='text/javascript'>
$(window).load(function(){
$("[lang='fa']").find("*").not(".dontChangeLanguage").contents().each(function() {
    if (this.nodeType === 3) 
{
        this.nodeValue = this.nodeValue.replace(/\d/g, function(v) {
            return String.fromCharCode(v.charCodeAt(0) + 0x06C0);
        });
    }
});
});
</script>
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<html>
<head>
</head>

<body>

<div lang="fa">

<div class="changeLanguage">some text here and 1234567890</div>

<table>
<tr>
<td class="dontChangeLanguage" id="apikey">123qwe456qwe123</td>
<td class="dontChangeLanguage" id="apikey">456ert456</td>
</tr>

</table>


</div>

</body>
</html>
&#13;
&#13;
&#13;