Home  >  Article  >  Web Front-end  >  No document.getElementByName method_javascript trick

No document.getElementByName method_javascript trick

WBOY
WBOYOriginal
2016-05-16 17:25:081408browse
The first statement is:
The document.getElementByName method does not exist. document.getElementsByName gets an array of tags
document.getElementId gets a certain tag



However, it can be obtained in a very simple way, such as:

var fn = document.getElementsByName("form_write")[0]; //Get this form The object under
fn.content.value;//just use this object to get the value.
document.getElementById 1. getElementById

Function: Generally, the ID is unique in the page, used to prepare to locate an element
Syntax: document.getElementById(id)
Parameter: id: required The option is string (String)
Return value: object; returns the first object with the same id, in the order of appearance on the page, if there is no object that meets the conditions, returns null

example :
Copy code The code is as follows:

document.getElementById("id1").value;


2. getElementsByName

Function: Search by the name of the element and return an array of elements with the same name
Syntax: document.getElementsByName( name)
Parameters: name: The required option is a string (String)
Return value: array object; if there is no object that meets the conditions, an empty array is returned, in the order of appearance on the page
Note: The returned array value is the value of the value attribute.
If a tag does not have a value attribute, getElementsByName can also get its value after you add the value attribute and assign a value.
When the value attribute is not assigned a value, getElementsByName returns an array. The value will be undefined,
but you can still get the number of the same name tag document.getElementsByName(name).length
document.getElementsByName can still be used when the name attribute is not set, it will get the value attribute based on your id The value of

example:
Copy the code The code is as follows:

document. getElementsByName("name1")[0].value;
document.getElementsByName("name1")[1].value;
All"
All"

span tag In fact, there are no name and value attributes
, but document.getElementsByName("CBylawIndexName") will still get the value of value

3. getElementsByTagName

Function: press HTML tag Name query, returns an array of elements with the same tag
Syntax: object.getElementsByTagName(tagname) object can be document or event.srcElement.parentElement, etc.
Parameters: tagname: The required option is a string (String), according to HTML Tag search.
Return value: array object; if there is no object that meets the conditions, an empty array is returned, in the order of appearance on the page

example:
Copy code The code is as follows:

document.getElementsByTagName("p")[0].childNodes[0].nodeValue;
document.getElementsByTagName( "p")[1].childNodes[0].nodeValue;
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn