javascript - 使用循环标记多个元素
问题描述
我目前正在尝试使用循环遍历多个输入框,但我不知道该怎么做。
我的 JS 代码:
function tagger() {
var t;
for (t = 0; t < 5; t++) {
document.getElementsByClassName("pl")[0].getElementsByTagName("input")
}
};
return tagger();
HTML:
<h1 class="pizza">Pineapple</h1>
<body class="maco">
<div class="keys">
<span class="press" onclick="car()"><i class="fas fa-fingerprint"></i><h2>Log In</h2></span>
<span class="type" id="heyu"><input class="entry" placeholder="Password"></span>
</div>
</body>
</html>
提前感谢您提供的任何帮助!
解决方案
您必须将 t 变量添加为 (getElementsByTagName) 的索引
document.getElementsByClassName("pl")[0].getElementsByTagName("input")[t];
或者您可以使用 forEach 方法是循环槽数组的最佳方法
function tragger () {
var _Element = Array.from(document.querySelector("pl input"));
_Element.forEach(function (ele) {
//do some code for each input
ele.value = 'etc';
});
}
tragger();
推荐阅读
- flutter - '动画
?不能分配给“Listenable”类型的变量 - php - FFMPEG 在本地主机和终端中工作,但不在实时网站上工作
- c++ - 我的班级是否有更好的对象内存对齐方式?
- c# - Wpf 用户控件的多个实例,都使用相同的路由事件
- database - 跨服务的增量序列
- javascript - 反应原生。后退按钮重定向到主页
- sql-server - PowerAutomate, SQL trigger : 修改项目时 (V2)
- reactjs - Slatejs:如何查询和更新现有节点?
- reactjs - 在 ReactJs 中提交表单数据后得到 UNDEFINED
- python - Python SFTP Paramiko:捕获错误的主机名错误