更新时间:2023-11-12 08:18:46
表单已提交,您的页面已重新加载。您需要停止表单提交。
The form is getting submitted and your page is reloaded. You need to stop the form submit.
解决方案1:添加类型
属性到你的按钮。
Solution 1: Add a type
attribute to your button.
< button type =buttononclick =removeP2();>删除< / button>
这将确保该按钮不是提交
类型(这是默认值如果未指定类型,则在表单标记内键入类型,并且单击时不提交表单。
This will make sure that the button is not of submit
type (which is the default type for buttons inside form tag when type is not specified), and that the form is not submitted on click.
解决方案2:防止在javascript中提交活动。所以进行这些更改。
Solution 2: Prevent the Submit event in javascript. So make these changes.
< button onclick =removeP2(event);>删除< / button>
并在脚本中阻止此事件
function removeP2(event) {
event.preventDefault(); // prevent the event , ie form submit event
var parent = document.getElementById("section");
var child = document.getElementById("p2");
parent.removeChild(child);
}
解决方案3:我认为不需要HTML中的表单标记。如果它是您的确切语法,并且您在表单提交中确实没有任何其他元素或目的,那么您可以删除表单
标记。
Solution 3: I dont see any need of the form tag over there in the HTML. If its the exact syntax of yours and you really dont have any other elements or purpose with the form submission then you can remove the form
tag.
<nav>
<button onclick="removeP2();">Remove</button>
</nav>