javascript - 使用名称设置新的 id 输入 javascript

标签 javascript input setattribute

我有输入类型文本

<input type="text" name="processName">

我想使用输入元素的名称为此输入元素设置新的 id

 document.getElementsByTagName("processName")[0].setAttribute("id", "proceesOffsetId");

哪个不起作用

最佳答案

所以getElementsByTagName指的是元素标签(例如:div标签、li标签、input标签等...)。它不会通过 name 属性获取元素。

因此,在您的情况下,要定位该 input 元素,您可以执行以下操作:

document.getElementsByTagName("input")[0].setAttribute("id", "proceesOffsetId");

这将为 input 元素提供 idproceesOffsetId

或者,要定位 name 属性,您可以使用 document.getElementsByName:

document.getElementsByName("processName")[0].setAttribute("id", "proceesOffsetId");

这还将为 input 元素提供 idproceesOffsetId

关于javascript - 使用名称设置新的 id 输入 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44298206/

相关文章:

php - 将值传递到灯箱

javascript - 如何切换到不同的日期格式?

javascript - 容器外的可选元素溢出 : hidden

python - 防止 Python 显示输入的输入

c - 如何在C89中管理终端unix中的unicode输入

python - 从函数返回用户提供的值

iframe - Mink/behat iframe 没有 id/name

Javascript 错误 : set audio object controls attribute True = = False

javascript - 如何将表单输入属性设置为计算的全局变量值

javascript - 使用 Parse.com 查询制作新闻源