gpt4 book ai didi

javascript - 如何将一个元素添加到另一个在 javascript 中动态创建的元素之上

转载 作者:行者123 更新时间:2023-12-03 00:05:50 25 4
gpt4 key购买 nike

我有一个按钮,允许显示更多字段,以便获取有关用户的补充信息(他的地址和电子邮件)。好吧,当我单击按钮时,它会添加字段,问题在于它们的位置,我希望它位于按钮上方而不是下方

enter image description here

这是代码

<html>

<head>
<script>
function addUser() {
let position2 = document.getElementById("position2");

var name = document.createElement("label");
position2.appendChild(document.createTextNode("name"));

var inputName = document.createElement("input");
inputName.setAttribute("type", "text");
position2.appendChild(inputName);

var retuurn = document.createElement("br");
position2.appendChild(retuurn);


var btn = document.createElement("input");
btn.setAttribute("type", "button");
btn.setAttribute("value", "add informations");
btn.setAttribute("onclick", "showMore()");
position2.appendChild(btn);
position2.appendChild(document.createElement("br"));
position2.appendChild(document.createElement("br"));
position2.appendChild(document.createElement("br"));
}



function showMore() {
var adress = document.createElement("label");
position2.appendChild(document.createTextNode("adress"));

var inputAdress = document.createElement("input");
inputAdress.setAttribute("type", "text");
position2.appendChild(inputAdress);
var retuurn = document.createElement("br");
position2.appendChild(retuurn);

var email = document.createElement("label");
position2.appendChild(document.createTextNode("email"));

var inputEmail = document.createElement("input");
inputEmail.setAttribute("type", "text");
position2.appendChild(inputEmail);

var retuurn = document.createElement("br");
position2.appendChild(retuurn);
var retuurn = document.createElement("br");
position2.appendChild(retuurn);
}
</script>
</head>

<body>
<form id="form">

<div id="position2"></div>

<button type="button" onclick="addUser()"> click to add user </button> <br><br>

</form>
</body>

</html>

最佳答案

您可以使用insertBefore而不是 appendChild。它需要第二个参数,在您的情况下应该是按钮元素。

为了获取对按钮的引用,我将通过脚本关联按钮的点击处理程序,而不是通过 onclick 属性。然后处理程序将获取事件对象,该对象的 target 属性中有按钮。

请参阅下面的更改:

<html>

<head>
<script>
function addUser() {
let position2 = document.getElementById("position2");

var name = document.createElement("label");
position2.appendChild(document.createTextNode("name"));

var inputName = document.createElement("input");
inputName.setAttribute("type", "text");
position2.appendChild(inputName);

var retuurn = document.createElement("br");
position2.appendChild(retuurn);


var btn = document.createElement("input");
btn.setAttribute("type", "button");
btn.setAttribute("value", "add informations");
btn.onclick = showMore; // <----
position2.appendChild(btn);
position2.appendChild(document.createElement("br"));
position2.appendChild(document.createElement("br"));
position2.appendChild(document.createElement("br"));
}



function showMore(e) { // <--- argument
var btn = e.target; // <--- get button
var adress = document.createElement("label");
// call insertBefore with second argument
position2.insertBefore(document.createTextNode("adress"), btn);

var inputAdress = document.createElement("input");
inputAdress.setAttribute("type", "text");
position2.insertBefore(inputAdress, btn);
var retuurn = document.createElement("br");
position2.insertBefore(retuurn, btn);

var email = document.createElement("label");
position2.insertBefore(document.createTextNode("email"), btn);

var inputEmail = document.createElement("input");
inputEmail.setAttribute("type", "text");
position2.insertBefore(inputEmail, btn);

var retuurn = document.createElement("br");
position2.insertBefore(retuurn, btn);
var retuurn = document.createElement("br");
position2.insertBefore(retuurn, btn);

btn.setAttribute('disabled', 'disabled');
}
</script>
</head>

<body>
<form id="form">

<div id="position2"></div>

<button type="button" onclick="addUser()"> click to add user </button> <br><br>

</form>
</body>

</html>

关于javascript - 如何将一个元素添加到另一个在 javascript 中动态创建的元素之上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54967730/

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