javascript javascript中getelementsbytagname和getelementsbyname有什么区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16664205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 05:33:14  来源:igfitidea点击:

what is the difference between getelementsbytagname and getelementsbyname in javascript

javascript

提问by Nav

I recently came across these two methods for dom elements retrieval in a douglas crockford presentation but couldn't understand the difference between these two.

我最近在 douglas crockford 的演讲中遇到了这两种检索 dom 元素的方法,但无法理解这两者之间的区别。

document.getElementsByTagName()

document.getElementsByName()

can someone please explain it to me.

有人可以向我解释一下吗。

the link to the video is http://www.youtube.com/watch?v=Fv9qT9joc0M

视频链接是http://www.youtube.com/watch?v=Fv9qT9joc0M

回答by Denys Séguret

Suppose you have this HTML :

假设你有这个 HTML :

<input name="test" class="cssclassname">

You'd got it with

你会得到它

document.getElementsByTagName('input')

or

或者

document.getElementsByName('test')

or

或者

document.getElementsByClassName('cssclassname')

Also, you can call getElementsByTagNameon elements other than document. For example the following is allowed,

此外,您还可以调用getElementsByTagName文档以外的元素。例如,以下是允许的,

document.getElementsById('foo').getElementsByTagName('bar')

But getElementsByNamecan only be called on document.

但是getElementsByName只能被调用document

Notes :

注意事项:

  • JavaScript is case sensitive, you can't write the functions like you did in your question
  • those functions don't return just the element but a live nodeList, so you'll have to iterate over the result or take the first one if you're sure it's good : document.getElementsByTagName('input')[0]
  • the MDN is a good documentation for JavaScript methods. You should read getElementsByTagNameand getElementsByName.
  • JavaScript 区分大小写,您不能像在问题中那样编写函数
  • 这些函数不只返回元素,而是返回一个活动的nodeList,因此如果您确定结果良好,则必须迭代结果或采用第一个:document.getElementsByTagName('input')[0]
  • MDN 是一个很好的 JavaScript 方法文档。您应该阅读getElementsByTagNamegetElementsByName

回答by Sani Singh Huttunen

<div name="alpha"></div>
<div name="beta"></div>

var divs = document.getElementsByTagName("div");  // Selects both divs.
var alpha = document.GetElementsByName("alpha");   // Selects the first div.
var beta = document.GetElementsByName("beta");     // Selects the second div.

回答by Jane Zangs

Wish I can help you, please check this

希望我能帮到你,请检查这个

<input type="checkbox" name="johnsmith" id="one">

in this case, input is the TagName, name is this name, id is the special unique identity.

在这种情况下,input 是 TagName,name 是这个名字,id 是特殊的唯一标识。

Let's me explain a better way,
"id" is your id card, there is only one able to hold this id card(id);
"name" is your name, but in USA has more than one person use this name which as john smith(name).
"input" is your category (TagName),4example human.
I hope you get it now. Good luck.

让我解释一个更好的方法,
“id”是你的身,只有一个人可以持有这张身(id);
“name”是你的名字,但在美国有不止一个人使用这个名字,比如 john smith(name)。
“input”是你的类别(TagName),4example human.
我希望你现在明白了。祝你好运。