gpt4 book ai didi

javascript - 需要切换 2 个 DIV 的 DIV 可见性

转载 作者:行者123 更新时间:2023-12-02 18:01:48 25 4
gpt4 key购买 nike

我需要显示一个包含英制尺寸和重量的 DIV,并有一个链接允许用户切换到显示公制的隐藏 DIV。尝试下面的标记,但它不起作用。它拒绝切换。我已经测试了第一个 div 没有给定样式及其样式“display: block;”两者都不起作用。我错过了什么?

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">

<style type="text/css">
<!--
body {
color:#000000;
background-color:#FFFFFF;
}
a { color:#0000FF; }
a:visited { color:#800080; }
a:hover { color:#008000; }
a:active { color:#FF0000; }
-->
</style>
<!--[if IE]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->

<script type="text/javascript">
function toggle_visibility(eng, met) {
var e1 = document.getElementById(eng);
var e2 = document.getElementById(met);
if(e1.style.display == 'block')
e1.style.display = 'none';
e2.style.display = 'block';
else
e1.style.display = 'block';
e2.style.display = 'none';
}
</script>
</head>
<body>
<div id="eng" style="display: block;">
This is English<br />
<a href="#" onclick="toggle_visibility('eng','met');">Convert to Metric</a>
</div>
<div id="met" style="display: none;">
This is Metric<br />
<a href="#" onclick="toggle_visibility('met','eng');">Convert to English</a>
</div>
</body>
</html>

最佳答案

试试这个 if 语句:

    if(e1.style.display == 'block') {
e1.style.display = 'none';
e2.style.display = 'block';
} else {
e1.style.display = 'block';
e2.style.display = 'none';
}

关于javascript - 需要切换 2 个 DIV 的 DIV 可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20460998/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com