Web 控制台中没有错误,代码用于隐藏一个元素并显示另一个元素。 stats 和 measure ID 在 html 中只出现一次。 a 和 b ID 也仅出现一次。 它在 Mozilla Firefox 中运行良好,但在 Chrome 中则不然。
这是代码。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>HeartBeat</title>
<style>
body {
background: #eee;
overflow-x: hidden;
margin: 0px;
}
.conta{
max-width: 750px;
margin-left: auto;
margin-right: auto;
}
.head {
background: #FF6969;
color: #fff;
font-size: 1.5em;
padding: 20px;
text-align: center;
}
.bd {
background: #fff;
padding: 10px;
}
.active {
border-bottom: 3px solid #FD0000;
}
#stats {
display: none;
background: #fff;
}
a {
text-decoration: none;
color: #423C3C;
font-weight: bold;
font-size: 1.4em;
}
a:hover {
border-bottom: 3px solid #FD0000;
}
h1 {
color:#667184;
}
h2 {
color:#D0D4DB;
}
.two-col {
display:block;
max-width: 50%;
min-width: 49%;
float: left;
text-align: center;
}
.three-col {
display:inline-block;
max-width: 33%;
min-width: 32%;
float: center;
text-align: center;
}
.text-center {
text-align: center;
}
.row {
max-width:100%;
}
.white-back {
background: #fff;
}
</style>
</head>
<body>
<div class="conta">
<div class="head">
<img src="h-1.png" width="50px" style="float:left; margin: -10px;"> Heart<b>Beat</b>
</div>
<div class="bd">
<div class="two-col"><a id="a" class="active" onClick='aTab()' href="#">Measure</a></div>
<div class="two-col"><a id="b" onClick='bTab()' href="#">Statistics</a></div>
<br/> <br/>
<div id="measure" class="text-center">
<img src="heart.jpg" width="90%">
<h1><span class="light">0</span>72</h1>
<h2>Beats Per Second</h2>
<img src="wave.png" alt="" width="90%" >
</div>
<div id="stats" class="text-center"> <br/>
<div class="row">
<div class="three-col"><a href="#">Day</a></div>
<div class="three-col"><a href="#">Month</a></div>
<div class="three-col"><a href="#">Year</a></div>
</div><div class="row"><br/> <br/>
<img src="cntr_img.jpg" alt="" width="80%"><br/>
<div class="three-col"><h2>Max</h2><h2>50</h2></div>
<div class="three-col"><h2>Min</h2><h2>157</h2></div>
<div class="three-col"><h2>Avg</h2><h2>81</h2></div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
function aTab() {
document.getElementById("stats").style="display: none;";
document.getElementById("measure").style="display: block;";
document.getElementById("b").className="";
document.getElementById("a").className="active";
}
function bTab() {
document.getElementById("stats").style="display: block;";
document.getElementById("measure").style="display: none;";
document.getElementById("b").className="active";
document.getElementById("a").className="";
}
</script>
</body>
</html>
我。已编辑代码。
最佳答案
您不应使用 style = "somestring"
,而应使用 style.someProperty = someValue
。
Here是一个在 chrome 中显示工作代码的 fiddle 。
代码如下(仅更改了 JS):
function aTab() {
document.getElementById("stats").style.display = "none";
document.getElementById("measure").style.display = "block";
document.getElementById("b").className="";
document.getElementById("a").className="active";
}
function bTab() {
document.getElementById("stats").style.display = "block";
document.getElementById("measure").style.display = "none";
document.getElementById("b").className="active";
document.getElementById("a").className="";
}
但是,我可能会稍微重写一下您当前的 JavaScript,以使其更加高效。 Here我通常会这样写类似的东西。代码在这里:
function GID(id) {
return document.getElementById(id);
}
var statsTab = GID("stats");
var measureTab = GID("measure");
var a = GID("a");
var b = GID("b");
function aTab() {
statsTab.style.display = "none";
measureTab.style.display = "block";
a.className = "active";
b.className = "";
}
function bTab() {
statsTab.style.display = "block";
measureTab.style.display = "none";
a.className = "";
b.className = "active";
}
关于JavaScript 可以在 Firefox 中运行,但不能在 Chrome 中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34565672/