Js notes (4) -- getAttributeNode () and setAttributeNode ()

Source: Internet
Author: User

1. Two days ago I wrote a piece of "analyzing the feature Attribute and Attribute Property of dom elements", and analyzed the differences between features and attributes. The article ignored a major knowledge point-getAttributeNode () and setAttributeNode (). Recently I read jQuery technology insider. Today I see jQuery. the part of attr () suddenly remembered this method. Let's talk about it briefly. 2. Start with jQuery: jQuery points out that in IE6 and 7, the getAttribute () and setAttribute () values of the browser cannot be normally obtained and set. JQuery tests are similar to div1.className = 'aaa'; alert (div1.getAttribute ("className") = 'aaa'); IE6, 7, or above, that is, the value cannot be obtained through the normal getAttribute ("class. In this case, the solution provided by jQuery is to get it through getAttributeNode ("class"). nodeValue. The related code is as follows: Copy code 1 if (! GetSetAttribute) {2 3 // omitting... 4 5 // Use this for any attribute in IE6/7 6 // This fixes almost every IE6/7 issue 7 nodeHook = jQuery. valHooks. button = {8 get: function (elem, name) {9 var ret; 10 ret = elem. getAttributeNode (name); 11 return ret & (fixSpecified [name]? Ret. nodeValue! = "": Ret. specified )? 12 ret. nodeValue: 13 undefined; 14}, 15 set: function (elem, value, name) {16 // Set the existing or create a new attribute node17 var ret = elem. getAttributeNode (name); 18 if (! Ret) {19 ret = document. createAttribute (name); 20 elem. setAttributeNode (ret); 21} 22 return (ret. nodeValue = value + ""); 23} 24}; 25 26 // omitted... 27} copy code 3. how to Apply: 3.1 getAttributeNode: getAttributeNode () is easy to use. It returns an Attr object, its nodeType = 2 <div id = "div1" class = "divClass"> </div> var className = div1.getAttributeNode ("class "). nodeValue; // 'divclass' var title = div1.getAttributeNode ("title"); // nu Llvar type = div1.getAttributeNode ("class "). nodeType; // 2 3.2 setAttributeNode: setAttributeNode () will receive an Attr-type object, which can be directly created using document: <div id = "div1" class = "divClass"> </div> var attr = document. createAttribute ("myAttr"); attr. nodeValue = 'wang'; div1.setAttributeNode (attr); run the above Code, and div1 will add a "myAttr" Custom feature: 4. finally, the analysis of getAttributeNode () and setAttributeNode () makes the analysis of html and dom attributes more comprehensive. If any problem is found, raise it! Thank you!

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.