gpt4 book ai didi

javascript - 为什么html中的Form Action 会自动触发?

转载 作者:行者123 更新时间:2023-11-28 05:48:47 24 4
gpt4 key购买 nike

<!DOCTYPE html>
<html>
<head>
<style>
button.accordion {
background-color: #eee;
color: #444;
cursor: pointer;
padding: 18px;
width: 100%;
border: none;
text-align: left;
outline: none;
font-size: 15px;
transition: 0.4s;
}

button.accordion.active, button.accordion:hover {
background-color: #ddd;
}

button.accordion:after {
content: '\02795';
font-size: 13px;
color: #777;
float: right;
margin-left: 5px;
}

button.accordion.active:after {
content: "\2796";
}

div.panel {
padding: 0 18px;
background-color: white;
max-height: 0;
overflow: hidden;
transition: 0.6s ease-in-out;
opacity: 0;
}

div.panel.show {
opacity: 1;
max-height: 500px;
}
</style>
</head>
<body>

<form action="page2.html">

<button class="accordion">Section 1</button>
<div class="panel">
Ch.1: <input type=checkbox id="ch1" ><br>
Ch.1: <input type=checkbox id="ch1"><br>
Ch.1: <input type=checkbox id="ch1"><br>
</div>

<button class="accordion">Section 2</button>
<div class="panel">
Ch.1: <input type="checkbox" id="ch1"><br>
Ch.1: <input type="checkbox" id="ch1"><br>
Ch.1: <input type="checkbox" id="ch1"><br>
</div>

<br>
<script>
var acc = document.getElementsByClassName("accordion");
var i;

for (i = 0; i < acc.length; i++) {
acc[i].onclick = function(){
this.classList.toggle("active");
this.nextElementSibling.classList.toggle("show");
}
}
</script>

</body>
</html>

当我尝试单击“第 1 节”或“第 2 节”时,页面会自动重定向到 page2.html。为什么会发生这种情况?使用相同的代码,我应该如何在“onsubmit”上添加java脚本函数?

最佳答案

您需要使用 type="button" 作为按钮标签,这样它就不会被视为导致表单操作执行的按钮提交

var acc = document.getElementsByClassName("accordion");
var i;

for (i = 0; i < acc.length; i++) {
acc[i].onclick = function(){
this.classList.toggle("active");
this.nextElementSibling.classList.toggle("show");
}
}
button.accordion {
background-color: #eee;
color: #444;
cursor: pointer;
padding: 18px;
width: 100%;
border: none;
text-align: left;
outline: none;
font-size: 15px;
transition: 0.4s;
}

button.accordion.active, button.accordion:hover {
background-color: #ddd;
}

button.accordion:after {
content: '\02795';
font-size: 13px;
color: #777;
float: right;
margin-left: 5px;
}

button.accordion.active:after {
content: "\2796";
}

div.panel {
padding: 0 18px;
background-color: white;
max-height: 0;
overflow: hidden;
transition: 0.6s ease-in-out;
opacity: 0;
}

div.panel.show {
opacity: 1;
max-height: 500px;
}
<form action="page2.html">

<button type="button" class="accordion">Section 1</button>
<div class="panel">
Ch.1: <input type=checkbox id="ch1" ><br>
Ch.1: <input type=checkbox id="ch1"><br>
Ch.1: <input type=checkbox id="ch1"><br>
</div>

<button type="button" class="accordion">Section 2</button>
<div class="panel">
Ch.1: <input type="checkbox" id="ch1"><br>
Ch.1: <input type="checkbox" id="ch1"><br>
Ch.1: <input type="checkbox" id="ch1"><br>
</div>
</form>

关于javascript - 为什么html中的Form Action 会自动触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38259071/

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