gpt4 book ai didi

javascript - 我正在尝试使用 div 名称作为变量来打开和关闭多个按钮的 div

转载 作者:行者123 更新时间:2023-12-01 00:11:57 25 4
gpt4 key购买 nike

我一直在试图解决这个问题,但我自己似乎无法做到这一点。如果我对 div 的名称进行硬编码并使用单个按钮,我有一个可以工作的函数。然而,我想用大约 10 个不同的按钮来完成此操作,并让它们使用相同的功能,而不是复制该功能 10 次。这是工作代码:

document.getElementById("genderBtn").addEventListener("click", myFunction);

function myFunction() {
var x = document.getElementById("gender");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
/* Checkbox Lineup */

fieldset.group {
margin: 0;
padding: 0;
margin-bottom: 1.25em;
padding: .125em;
}

fieldset.group legend {
margin: 0;
padding: 0;
font-weight: bold;
margin-left: 20px;
font-size: 100%;
color: black;
}

ul.checkbox {
margin: 0;
padding: 0;
margin-left: 20px;
list-style: none;
}

ul.checkbox li input {
margin-right: .25em;
}

ul.checkbox li {
border: 1px transparent solid;
display: inline-table;
width: 12em;
}

ul.checkbox li label {
margin-left: ;
}

ul.checkbox li:hover,
ul.checkbox li.focus {
background-color: lightyellow;
border: 1px gray solid;
width: 12em;
}


/* Radio button lineup */

ul.radio {
margin: 0;
padding: 0;
margin-left: 20px;
list-style: none;
}

ul.radio li input {
margin-right: .25em;
}

ul.radio li {
border: 1px transparent solid;
display: inline-table;
width: 12em;
}

ul.radio li label {
margin-left: ;
}

ul.radio li:hover,
ul.radio li.focus {
background-color: lightyellow;
border: 1px gray solid;
width: 12em;
}

hr {
display: block;
height: 1px;
border: 0;
border-top: 1px solid #ccc;
margin: 1em 0;
padding: 0;
}
<fieldset class="group">
<legend>
<h1>Demographics <button type="button" id="genderBtn">Gender</button></h1>
</legend>

<ul class="checkbox">
<li><label for="name">Name: </label><input type="text" name="" id="name" value="" placeholder="default: client" /></li>
<li><label for="age">Age: </label><input type="text" name="" id="age" value="" /></li><br>
</ul>
<hr />
<div id="gender">
<b>Biological Sex</b>
<ul class="checkbox">
<li><input type="checkbox" name="gender[]" id="gender0" value="female" /><label for="gender0">Female</label></li>
<li><input type="checkbox" name="gender[]" id="gender1" value="male" /><label for="gender1">Male</label></li><br>
</ul>
<hr />
</div>
<b>Ethnic/Racial Information</b>
<ul class="checkbox">
<li><input type="checkbox" name="race[]" id="race0" value="Caucasian" /><label for="race0">Caucasian</label></li>
<li><input type="checkbox" name="race[]" id="race1" value="African-American" /><label for="race1">African-American</label></li>
<li><input type="checkbox" name="race[]" id="race2" value="Hispanic" /><label for="race2">Hispanic</label></li>
<li><input type="checkbox" name="race[]" id="race3" value="Arabic" /><label for="race3">Arabic</label></li>
<li><input type="checkbox" name="race[]" id="race4" value="Asian" /><label for="race4">Asian</label></li>
<li><input type="checkbox" name="race[]" id="race5" value="multi-racial" /><label for="race5">Multi-racial</label></li>
<li><label for="race6">Other:</label><input type="text" name="" id="race6" value="" /></li>
</ul>
</fieldset>

现在,当我尝试更改它以将 div 名称作为变量传递时,我似乎无法让它工作。

document.getElementById("someOtherBtn").addEventListener("click", myFunction(someOtherDiv));

function myFunction(theDivName) {
var x = document.getElementById("theDivName");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}

有人可以让我知道我做错了什么吗?

最佳答案

未测试:

document.getElementById("someOtherBtn").addEventListener("click", function() {
myFunction("someOtherDiv");
});

function myFunction(theDivName) {
var x = document.getElementById(theDivName);
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}

或者

myClickFunction("someOtherDiv");
myClickFunction("someOtherDiv2");
myClickFunction("someOtherDiv3");

function myClickFunction(theDivName) {
var x = document.getElementById(theDivName);
x.addEventListener("click", function() {
myFunction(x);
});
}
function myFunction(theDivElement) {
var x = theDivElement;
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}

关于javascript - 我正在尝试使用 div 名称作为变量来打开和关闭多个按钮的 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60022413/

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