Javascript getattribute () method

Source: Internet
Author: User
Getattribute () method

● A document is a node tree.

● Nodes are divided into different types: Element nodes, attribute nodes, and text nodes.

● The getelementbyid () method returns an object that corresponds to a specific element node in the document.

● The getelementsbytagname () method returns an array of objects corresponding to a specific element node in the document.

● Each of these nodes is an object.

Next, we will introduce several attributes and methods associated with these objects.

3.4.1 getattribute () method

So far, we have introduced two methods to retrieve specific element nodes: getelementbyid () and getelementsbytagname. After finding the element, we can use the getattribute () method to query the values of its various attributes.

The getattribute () method is a function. It has only one parameter -- the name of the attribute you want to query:

Object. getattribute (attribute)
However, the getattribute () method cannot be called through the document object, which is different from other methods we have previously introduced. We can only call it through one Element Node object.

For example, you can combine it with the getelementsbytagname () method to query the title attribute of each <p> element, as shown below:
VaR text = Document. getelementsbytagname ("p ")
For (VAR I = 0; I <text. length; I ++)
{
Alert (Text [I]. getattribute ("title "));

}
If the preceding SectionCodeInsert it to the end of the preceding "shopping list" sample document and reload the page in the Web browser, on the screen, an alter dialog box displaying the text message "a gentle reminder" is displayed.

There is only one <p> element with the title attribute in the "shopping list" document. If this document contains one or more <p> elements without the title attribute, the corresponding getattribute ("title") Call will return null. Null is a null value in Javascript. It indicates that "the things you mentioned do not exist ". If you want to verify this in person, first Insert the following text into the existing text section in the shopping list document:
<P> This is just test </P>
Then reload the page. This time, you will see two alter dialogs, the second dialog box is blank or only displays the word "null". The actual situation depends on how your web browser displays the null value.

You can modify the script so that a message is displayed only when the title attribute exists. We will add an if statement to check whether the return value of the getattribute () method is null. With this opportunity, we have added several variables to improve the readability of the script:
VaR Ts = Document. getelementsbytagname ("Li ");
For (VAR I = 0; I <ts. length; I ++)
{Text = ts [I]. getattribute ("title ");

If (text! = NULL)
{
Alert (text)
}
}
Now, if you reload this page, you will only see an alter dialog box that displays the "a gentle reminder" message, as shown below.

We can even shorten this code. When checking whether a data item is null, we are actually checking whether it exists. This check can be simplified to the condition that the checked data is directly used as the if statement. If (something) And if (something! = NULL) is completely equivalent, but the former is obviously more concise. If something exists, the condition of the IF statement is true. If something does not exist, the condition of the IF statement is false.

In this example, we only need to set if (title_text! = NULL) with if (title_text), we can get more concise code. In addition, to further increase the readability of the code, we can take this opportunity to write the alter statement and the IF statement on the same line, which makes them closer to the English sentence in our daily life:
VaR Ts = Document. getelementsbytagname ("Li ");
For (VAR I = 0; I <ts. length; I ++)
{Text = ts [I]. getattribute ("title ");

If (text) Alert (text)

}
3.4.2 setattribute () method

All methods we introduced earlier can only be used to retrieve information. The setattribute () method is essentially different from the setattribute () method: it allows us to modify the value of the attribute node.

Similar to the getattribute () method, the setattribute () method is also a function that can only be called through an Element Node object. However, the setattribute () method needs to pass two parameters to it:

Obiect. setattribute (attribute, value)
In the following example, the first statement retrieves the element whose ID attribute value is purchase, and the second statement sets the title attribute value of this element to a list of goods:

VaR shopping = Document. getelementbyid ("purchases ")
Shopping. setattribute ("title", "a list of goods ")
We can use the getattribute () method to prove that the title attribute value of this element has indeed changed:
VaR shopping = Document. getelementbyid ("purchases ");
Alert (shopping. getattribute ("title "));
Shopping. setattribute ("title", "a list of goods ");
Alert (shopping. getattribute ("title "));
The preceding statements will pop up two alert dialogs on the screen: The first alter dialog box appears before the setattribute () method is called, it will be blank or the word "null" is displayed "; the second message appears after the title attribute value is set. It displays the "A List of Goods" message.

In the preceding example, we set the title attribute of an existing node, but this attribute does not exist. This means that the setattribute () call has actually completed two operations: create this attribute first, and then set its value. If we use the setattribute () method on an existing attribute of an element node, the current value of this attribute will be overwritten.

In the example document of "shopping list", the <p> element already has a title attribute. The value of this attribute is a gentle reminder. We can use the setattribute () method to change its current value:

<SCRIPT type = "text/JavaScript">
VaR Ts = Document. getelementsbytagname ("Li ");
For (VAR I = 0; I <ts. length; I ++)
{
VaR text = ts [I]. getattribute ("title ");
Alert (TS [I]. getattribute ("title "))
If (text)
{
TS [I]. setattribute ("title", "I will succeed! ")
Alert (TS [I]. getattribute ("title "))
}
}
The above code first Retrieves all the <p> elements with the title attribute from the document, and then modifies all their title attribute values to brand new title text. In the "shopping list" document, the attribute value a gentle reminder will be overwritten.

here is a very noteworthy detail: the modifications made to the document through the setattribute () method, this will change the Display Effect and/or action of the document in the browser window, however, we can view the source code in the browser (view Source Code ) when the option is used to view the source code of the document, the original property value is still displayed. That is to say, the changes made by the setattribute () method are not reflected in the source code of the document. This "table is different" phenomenon comes from Dom's working mode: loading static content of documents first, and then refreshing them dynamically. Dynamic Refresh does not affect static content of documents. This is the real power and attraction of DOM: Refreshing page content does not require end users to perform page refresh operations in their browsers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.