JSAttribute attribute Operation Details _ basic knowledge

Source: Internet
Author: User
The following is a detailed description of JSAttribute attribute operations. I think this is quite good. Now I will share it with you and give you a reference. Let's take a look at the meaning of Attribute. This article only introduces some attributes compatible with IE and FF.

Attributes: Get an attribute as an object

GetAttribute: Get the value of an attribute.

SetAttribute: creates an attribute and binds a value to the attribute.

CreateAttribute: creates only one attribute.

RemoveAttribute: deletes an attribute.

GetAttributeNode: Get a node as an object

SetAttributeNode: Create a node

RemoveAttributeNode: deletes a node.

Attributes can get an attribute of an object and call it as an object. Note that "[]" should be used here. IE can use "()" here, considering compatibility issues, use "[]". There are huge differences between IE and FF on how to use attributes. We will not discuss them here.

How to use attributes: (General for IE and FF)

Script var d = document. getElementById ("sss "). attributes ["value"]; document. write (d. name); document. write (d. value); // display value aaa script

GetAttribute, setAttribute, createAttribute, and removeAttribute are easy to understand and use in a simple way. Note the following points:

1. When createAttribute is used, it does not need to be object-based. document. createAttribute.

2. When using setAttribute and createAttribute, do not use words such as name, type, and value. The response of IE and FF is strange and hard to understand.

3. When using createAttribute, if only the name is defined, No d. nodeValue = "hello"; the statement defines the value. FF will regard it as an empty string, and IE will regard it as undefined. Just pay attention to this point.

GetAttribute usage:

Script var d = document. getElementById ("sss"). getAttribute ("value"); document. write (d); // display aaa script

SetAttribute usage: (you will find an additional property named "hello)

《script》var d = document.getElementById("sss").setAttribute("good","hello");alert(document.getElementById("t").innerHTML)《script》

CreateAttribute usage: (an empty attribute named good is added)

    
     Script window. onload = function () {var oBox = document. getElementById ('box'); alert (document. body. innerHTML); oBox. setAttribute ('value', 'name'); alert (document. body. innerHTML); attr = document. createAttribute ('Allo'); alert (document. body. innerHTML);/* same as above */attr. nodeValue = 'World';/* edit Custom Attributes */alert (document. body. innerHTML);/* same as above */oBox. setAttributeNode (attr);/* Insert custom attributes to tags */alert (document. body. innerHTML);/* change */}; script      
 
 

RemoveAttribute usage: (one missing)

《script》var d = document.getElementById("sss").removeAttribute("value");alert(document.getElementById("t").innerHTML)《script》

The getAttributeNode, setAttributeNode, and removeAttributeNode methods all operate on a node directly. removeAttributeNode always uses an error at the beginning, but fully understands the meaning of node, you can use it freely.

How to Use getAttributeNode:

Script var d = document. getElementById ("sss "). getAttributeNode ("value"); document. write (d. name); document. write (d. value); // display value aaa script

SetAttributeNode usage:

《script》var d = document.createAttribute("good");document.getElementById("sss").setAttributeNode(d);alert(document.getElementById("t").innerHTML);《script》

How to Use removeAttributeNode:

《script》var d = document.getElementById("sss").getAttributeNode("value")document.getElementById("sss").removeAttributeNode(d); alert(document.getElementById("t").innerHTML);《script》

For more information about the mandatory attributes of attributes, see w3school!

The details of the JS Attribute operation above are all the content that I have shared with you. I hope you can give me a reference and support me a lot.

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.