我有输入类型文本
<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
元素提供 id
为 proceesOffsetId
。
或者,要定位 name
属性,您可以使用 document.getElementsByName
:
document.getElementsByName("processName")[0].setAttribute("id", "proceesOffsetId");
这还将为 input
元素提供 id
为 proceesOffsetId
。
关于javascript - 使用名称设置新的 id 输入 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44298206/