首页 > 解决方案 > 使用 javascript 函数将一个 unicode 转换为另一个

问题描述

目标:单击时将箭头更改为向下。

我尝试在 Javascript 中使用不同的 unicode 来更改它,但即使通过“\”转义,我也只能得到基本字符串......

小失落。希望这是一个简单的问题。

$('.col-exp').on('click', function() {
  let th = $(this);
  th.children('span').text('***Change to down arrow***');
  th.next('section').slideToggle();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h2 class="col-exp">Employment<span>&#11206;</span></h2>
<section id="employment">
  <table>
    <tr>
      <td>FakeSystems Inc</td>
      <td>Master Developer</td>
      <td>2000-2010</td>
      <td>Managed API routing by coordinating with foo and assessing the accuracy of returned data from 3rd party partners</td>
    </tr>
    <tr>
      <td>WorseSystems Inc</td>
      <td>Lesser Developer</td>
      <td>1990-2000</td>
      <td>Made sites with Geocities and Angelfire that included tons of <code>blink</code> tags and gifs to make me look awesome</td>
    </tr>
  </table>
</section>
<h2 class="col-exp">Education<span>&#11206;</span></h2>
<section id="education">
  <table>
    <tr>
      <td>Fake Institute of Fortitude</td>
      <td>1986-1990</td>
    </tr>
  </table>
</section>
</div>
</div>

标签: javascriptjqueryunicode

解决方案


您需要使用.html,因为它是 HTML 实体之一。

使用.text,它将被视为纯文本,不会被解析为 HTML。

$('.col-exp').on('click', function(){
  let th = $(this);
  th.children('span').html('&#11205;');
  th.next('section').slideToggle();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<h2 class="col-exp">Employment<span>&#11206;</span></h2>
<section id="employment">
  <table>
    <tr>
      <td>FakeSystems Inc</td>
      <td>Master Developer</td>
      <td>2000-2010</td>
      <td>Managed API routing by coordinating with foo and assessing the accuracy of returned data from 3rd party partners</td>
    </tr>
    <tr>
      <td>WorseSystems Inc</td>
      <td>Lesser Developer</td>
      <td>1990-2000</td>
      <td>Made sites with Geocities and Angelfire that included tons of <code>blink</code> tags and gifs to make me look awesome</td>
    </tr>
  </table>
</section>
<h2 class="col-exp">Education<span>&#11206;</span></h2>
<section id="education">
  <table>
    <tr>
      <td>Fake Institute of Fortitude</td>
      <td>1986-1990</td>
    </tr>
  </table>
</section>


推荐阅读