gpt4 book ai didi

javascript - 在这种情况下如何调用输入?

转载 作者:行者123 更新时间:2023-12-02 21:10:04 25 4
gpt4 key购买 nike

我想为这种情况下的每个输入提供一个类,但我不知道如何做。

JS:

  document.getElementsByClassName("parent")[0].addEventListener("click",function(){ document.getElementsByClassName("maco")[0].innerHTML="<form><wrap>Name: <input></wrap><wrap>Date Range from <input> to <input></wrap><wrap>Reason:<input></wrap><wrap>Name of Parent:<input></wrap><wrap><button>Enter<button></wrap></form>";
document.getElementsByClassName("parent")[0].getElementsByTagName("form")[0].classList.add("pl");
document.getElementsByClassName("parent")[0].getElementsByTagName("form")[0].getElementsByTagName("input")[0].classList.add("first");
document.getElementsByClassName("parent")[0].getElementsByTagName("form")[0].getElementsByTagName("input")[1].classList.add("second");
document.getElementsByClassName("parent")[0].getElementsByTagName("form")[0].getElementsByTagName("input")[2].classList.add("third");
document.getElementsByClassName("parent")[0].getElementsByTagName("form")[0].getElementsByTagName("input")[3].classList.add("fourth");
document.getElementsByClassName("parent")[0].getElementsByTagName("form")[0].getElementsByTagName("input")[4].classList.add("fifth"); });

我执行了上述操作,但浏览器一直显示 Uncaught TypeError: Cannot read property 'getElementsByTagName' of undefined

有没有办法为innerHTML =“...”中的引用中的每个输入提供一个类?

最佳答案

由于您没有发布 HTML 结构,因此很难确定这是否有效,但假设您的树如下所示:

<div class="parent">
<form>
<input>
<input>
<input>
</form>
</div>

我将这样做。

const numericalWords = [
"first",
"second",
"third"
]


document.querySelectorAll('.parent form input').forEach((input, i) => {
input.classList.add(numericalWords[i])
})
.first {
background: yellow;
}

.second {
background: blue;
}

.third {
background: green;
}
<div class="parent">
<form>
<input>
<input>
<input>
</form>
</div>

关于javascript - 在这种情况下如何调用输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61123394/

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