将华氏字符更改为celcius字符

时间:2016-12-01 15:07:54

标签: jquery

enter image description here

我想实现这个目标,当我点击华氏字符时,它会变成Celcius字符。但是不行,怎么做?

<html lang="en">

$("a").click(function() {
  if ($(this).text() == "&#8457;") {
    $(this).html("&#8451;");
  } else {
    $(this).html("&#8457;");
  }
});
<script src="https://cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.css">
<link rel="stylesheet" href="css/owfont-regular.css">
<a id="toggle" href="javascript:void(0)">&#8457;</a>

1 个答案:

答案 0 :(得分:3)

这对我有用

$("a").click(function() {
  if ($(this).text() == "℉") {
    $(this).text("℃");
  } else {
    $(this).text("℉");
  }
});
<script src="https://cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>
<a id="toggle" href="javascript:void(0)">℉</a>

这样做

$("a").click(function() {
  var temp = $(this).text();
  $(this).text(temp == "℉"?"℃":"℉");
});
<script src="https://cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>
<a id="toggle" href="javascript:void(0)">℉</a>