document.getElementsByTagName 不起作用

2022-01-18 00:00:00 tags javascript

我想使用 document.getElementsByTagName('input')根据需要设置(或取消设置)输入列表.有可能吗?

I would like to use document.getElementsByTagName('input') to set as required (or unset it) for a list of inputs. is it possible?

我试过了:

document.getElementsByTagName('input').required = false;

或(用于不同的目的)

document.getElementsByTagName('input').value = ""

但它似乎不起作用.

此外:是否可以捕获某种类型的输入(即文本或广播)?

Moreover: is it possible to catch a certain type of input (i.e. text or radio)?

谢谢!!!ObOnKen

Thank you!!! ObOnKen

推荐答案

getElementsByTagName() 返回一个元素集合,所以你需要遍历集合...

getElementsByTagName() returns a collection of elements so you need to iterate over the collection...

var elements = document.getElementsByTagName('input');

for(var i = 0; i < elements.length; i++)
{
    if(elements[i].type == "text")
    {
        elements[i].value = "";
    }
}

相关文章