


During this period of free time in the company, I decided to study JavaScript access to HTML controls. This is very common. Here I systematically researched JavaScript access methods. I passed the test and have the following research achievements for everyone to share and supplement. .
Let’s get to the point:
The main object of access control is: document object. Corresponds to all (sub-objects) personal views of the current document. And several main methods have been provided to access the object. " 🎜> 1 . First let me talk about the usage of document.getElementById.
Var obj=document.getElementById("ID") Gets the object based on the specified ID attribute value. Returns a reference to the first object whose id attribute value is equal to ID. If the corresponding object is a group of objects, the first object in the group is returned.
)”)" I tested the above code in IE, enter 1 in the first text box, and enter 1 in the second text box. 2. Then click two buttons to eat a pound. As a result, both buttons return the value of the first text box. This shows that when IE executes document.getElementById(elementName), it returns the first object whose name or id is equal to elementName, and does not search based on the ID.
But on the contrary, I don’t have this problem in firefox. When Firefox executes document.getElementById(elementName), it can only find the object whose ID is equal to elementName. If it does not exist, it will return null.
2. Let's take a look at the usage of document.getElementsByName.
Var obj=document.getElementsByName("Name") gets the object collection based on the value of the Name attribute. Returns a collection of objects whose name is equal to the specified Name. Note that what is returned here is a set, including the case where there is only one element.
document.getElementsByName("name")[0?1?2?3?....] This is how to get a certain element. Note that you can use [] or () to get a value from a set in JavaScript (I passed the test, but there is no information to write it this way).
For example:
<script> <br/>function prop() <br/>{ <br/>var objs=document.getElementsByName("a"); <br/>alert(objs(0).value) ;//Or you can alert(objs[0].value) which is also correct. <br/>} <br/></script>
3. Usage of Document.getElementsByTagName:
Var ojbs=document.getElementsByTagName("Tag") based on a collection of objects based on the specified element name. Returns a collection whose Tag property is equal to the specified Tag tag. Also returned here is a collection. (Same as above)
4. document.all usage.
document.all is a collection of all elements in the page. For example:
document.all(0) represents the first element of the page.
Document.all("txt") represents single elements and collection elements of all objects on the page whose id or name is equal to txt.
If the id or name on the page is equal to txt and has only one element (including name and id), then the result of document.all() is only one element, otherwise it is a collection. (Integrating the respective characteristics of document.getElementById and document.getElementsByName).
You can also write it like this: document.all.txt is the same.
For example:
Code 2:
But often name can Same (such as: using checkbox to retrieve multiple hobbies of the user)
Theoretically, the IDs in a page are different from each other. If different tags appear with the same id, document.all.id will fail, like this:
In addition, document.all can determine whether the browser type is IE,
document.all---- -----For IE
document.layers------------For Netscape
These two collections .all are only valid in ie, layers are only valid in nc
So you can judge different browsers in this way.
Finally, let me talk about the usage scope of getElementById and getElementsByName:
Id is like an ID number, it is unique, and name can be the same as a name.
An element defines an id. When referencing the element, use the id attribute directly. The name is usually used in form and must come from document.form.***. In other words, the element defined by the name attribute is in the script. is a sub-object of the document object.
1. name is used for elements inside the form, which is required for submission.
id is easy to use for elements outside the form because DOM can directly obtain a single element
2. There can only be one id per page
name is OK There are multiple names and some tags are not recommended to use it
3. Form elements (form input textarea select) and frame elements (iframe frame) use name. These elements are related to the form (frame element acts on the target of the form) submission. In The receiving page of the form only accepts elements with name. Elements assigned ID cannot receive values through the form. You can verify it yourself. There is an exception. A can assign name as an anchor point, or it can assign ID; it can only assign ID but not Elements assigned name: (except for elements related to the form, only ID can be assigned) body li table tr td th p p span pre dl dt dd font b and so on.
Here I raise another question. Since you have an ID, why do you need a name?
The most direct answer: ID is like a person’s ID number, and name is like his name. Although the ID is Unique, but name can be repeated.
Specifically: for ID, it is the Identity of the HTML element on the client side. Name is actually much more complicated, because Name has many uses, so it cannot be completely replaced by ID, thus canceling it.
Reference website information is as follows: specific uses are:
Purpose 1: As a server-side identifier of HTML elements that can interact with the server, such as input, select, textarea, and button, etc. We can get the value submitted by the element on the server side based on its Name through Request.Params. Usage 2: HTML element Input type = "radio" grouping, we know that radio button controls are in the same grouping class, the check operation is mutex, and only one radio can be selected at the same time. This grouping is implemented based on the same Name attribute.
Purpose 3: Establish an anchor point in the page. We know that link is to obtain a page hyperlink. If you do not use the href attribute, use Name instead, such as : , we get a page anchor.
Usage 4: Identity as an object, such as Applet, Object, Embed and other elements. For example, in the Applet object instance, we will use its Name to refer to the object.
Purpose 5: When linking IMG elements and MAP elements, if you want to define the hotspot area of IMG, you need to use its attribute usemap, useusemap="#name"(Name of the associated MAP element)
Usage 6: Attributes of certain specific elements, such as attribute , and param . For example, define parameters for Object .
Obviously these uses cannot be simply replaced by ID, so the ID and Name of the HTML element are not the same. It's not the difference between ID number and name, they have different functions. Of course, the Name attribute of the HTML element can also play a role as an ID in the page, because in the DHTML object tree, we can use document.getElementsByName to obtain an object array containing all specified Name elements in the page.
By the way, what if there are n (n >1) HTML elements in the page with the same ID? How to reference them in DHTML object? If we use ASPX pages, this situation is not easy to happen, because the aspnet process does not allow non-unique IDs when processing aspx pages. This means that the page will throw an exception and cannot be rendered normally. If it is not a dynamic page and we insist on repeating the ID, what should we do with IE?
At this time we can still use document.getElementById to obtain objects, but we can only obtain the first object that appears during HTML Render among those objects with duplicate IDs. At this time, the repeated ID will automatically become an array when referenced, and the elements with repeated IDs will exist in the array in order of Render.
getElementById("xxx") returns the first element whose id attribute is "xxx" or a specific form element name is "xxx"
getElementsByName("xxx") returns all elements whose id attribute is "xxx" or a specific form Element whose element name is "xxx"
Let me explain here that the range of elements obtained by getElementById and getElementsByName is the same. The difference is that the former only returns the first element and the latter returns the collection of all elements.
In addition, the form element refers to the form element in

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
