gpt4 book ai didi

Javascript 切换元素的显示不工作

转载 作者:太空宇宙 更新时间:2023-11-03 20:23:10 26 4
gpt4 key购买 nike

我已经这样做了几十次,但出于某种原因,我无法弄清楚为什么在单击 #dropdown-button 时没有显示 #dropdown-list div...

<html>
<head>
<script type="text/javascript">
function toggleDropdown(optionalArg){
optionalArg = (typeof optionalArg == "undefined")?'defaultValue':optionalArg
ele = document.getElementById("dropdown-list");
//If optionalArg is 0 we want to hide it
if(optionalArg == 0){
ele.style.display=='none'
}
//If optionalArg is 1 we want to show it
else if(optionalArg == 1){
ele.style.display=='block';
}
//If optionalArg is NULL we want to toggle it's display
else{
if(ele.style.display=='none')
ele.style.display=='block';
else
ele.style.display=='none';
}
}
</script>
<style type="text/css">
#dropdown-container{
width:200px;
border:1px solid black;
overflow: hidden;
}
#dropdown-text {
width:176px;
float:left;
}
#dropdown-button {
text-align:center;
width:20px;
border:2px outset black;
float:left;
}
#dropdown-button:active {
border:2px inset black;
}
#dropdown-list {
display:none;
width:200px;
max-height:250px;
background-color:#b0c4de;
border:1px solid #cccccc;
overflow:auto;
}
</style>
</head>
<body>
<div id="dropdown-container">
<div id="dropdown-label">
<input id="dropdown-text" type="text" />
<div id="dropdown-button" onclick="toggleDropdown();">V</div>
</div>
<div id="dropdown-list">
<ul class="dropdown-ul">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</div>
</div>
</body>

应该很简单,但我认为现在已经晚了,我对这个简单的 JS 有问题。感谢您查看!

最佳答案

您使用相等运算符 (==) 而不是赋值 (=):

        if(ele.style.display=='none')
ele.style.display=='block';
else
ele.style.display=='none';

应该是

        if(ele.style.display=='none')
ele.style.display='block';
else
ele.style.display='none';

关于Javascript 切换元素的显示不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5523201/

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