我想实现这个目标,当我点击华氏字符时,它会变成Celcius字符。但是不行,怎么做?
<html lang="en">
$("a").click(function() {
if ($(this).text() == "℉") {
$(this).html("℃");
} else {
$(this).html("℉");
}
});
<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)">℉</a>
答案 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>