Example of using the GetAttribute method in JS

Source: Internet
Author: User

The GetAttribute () method is a Function. It has only one parameter--the name of the property you intend to query, and the following describes its specific use

GetAttribute () method

So far, We've introduced two ways to retrieve specific element nodes: one is to use the getElementById () method, and the other is to use the getElementsByTagName () method. After finding that element, we can use the GetAttribute () method to query the values of its various properties.
The GetAttribute () method is a Function. It has only one parameter-the name of the property you intend to query:
Object.getattribute (attribute)
however, the GetAttribute () method cannot be called through the document object, unlike the other methods we have described earlier. We can call it only through an element node Object.
For example, you can combine it with the getElementsByTagName () method to query the title property of each <p> element, as Follows:

?
12345 var text=document.getelementsbytagname ( " p " ) for ( var i=0;i<text.length;i++) { alert (text[i].getattribute ( "title"

If you insert the above code at the end of the "shopping list" sample document given earlier and reload the page in a Web browser, an Alter dialog box appears with the text message "a gentle reminder" displayed.
There is only one <p> element with the title attribute in the shopping list Document. If the document also has one or more <p> elements without the title attribute, then the corresponding getattribute ("title") call will return Null. NULL is a null value in the JavaScript language, meaning "this thing you say does not exist". If you want to verify this yourself, first insert the following text into the existing text paragraph in the shopping list document:
<p>this is just test</p>
Then reload the Page. This time, you will see two alter dialogs, and the second will be blank or just the word "null"-depending on how your Web browser will display null Values.
You can modify our script so that it pops up a message only when the title property is Present. We will add an if statement to check if the return value of the GetAttribute () method is Null. Taking advantage of this opportunity, we have also added several variables to improve the readability of the script:

?
12345678 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 shows a gentle reminder message, as shown below.
We can even shorten the code a little Bit. When checking whether an item is a null value, we are actually checking to see if it exists. This check can be simplified to the condition that the checked data is used as an if statement directly. If (something) is exactly equivalent to if (something! = null), But the former is clearly more concise. At this point, if the something exists, the condition of the IF statement will be true, and if the something does not exist, the condition of the IF statement will be False.
In this case, as long as we replace 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 also take this opportunity to write the ALTER statement on the same line as the if statement, which allows them to be closer to the English sentence in our daily life:

?
12345 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 of the methods we've introduced to you have been used only to retrieve Information. The SetAttribute () method has a fundamental difference from them: it allows us to make changes to the value of the attribute Node.
Similar to the getattribute () method, the SetAttribute () method is also a function that can be called only through an element node object, but the SetAttribute () method requires that we pass two parameters to It:
Obiect.setattribute (attribute,value)
In the following example, the first statement retrieves the element that the id attribute value is purchase, and the second statement sets the value of the title property of the element to a list of goods:

?
12 varshopping=document.getElementById("purchases")shopping.setAttribute("title","a list of goods")

We can use the GetAttribute () method to prove that the value of the title property of this element has indeed changed:

?
1234 var shopping=document.getelementbyid ( "purchases" alert (shopping.getattribute ( shopping.setattribute ( "a list of goods" alert (shopping.getattribute (

The above 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, and the second appears after the title property value is set, it will show "a List of goods "messages.
In the example above, we have set the title property of an existing node, but this property does not exist originally. This means that the setattribute () call we make actually accomplishes two things: create the property first, and then set the Value. If we use the SetAttribute () method on an existing property of an element node, the current value of this property will be Overwritten.
In the shopping list sample document, the,<p> element already has a title property, and the value of this property is a gentle reminder. We can use the SetAttribute () method to change its current value:

?
123456789101112 <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","我会成功!")alert(ts[i].getAttribute("title"))}}

The above code will first retrieve all the <p> elements with the title attribute from the document, and then modify all of their title property values to brand new title Text. Specific to the "shopping list" document, the property value a gentle reminder will be Overwritten.
Here's a very interesting detail: changes made to the document through the SetAttribute () method will change the way the document appears in the browser window and/or behavior, but we are in the Browser's view SOURCE (view source) option to see the source code of the document will still be the original property Value-that is, The changes made by the SetAttribute () method are not reflected in the source code of the document ITSELF. This "duplicity" phenomenon stems from the Dom's working mode: loading the static content of the document, and then dynamically refreshing them, dynamic refresh does not affect the static content of the Document. This is the real power and allure of dom: the refresh of page content does not require the end user to perform a page refresh in their browser.

Example of using the GetAttribute method in JS

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.