Details about deleting attributes and adding attributes in js

Source: Internet
Author: User

For adding and deleting attributes in js, we mostly set the element attributes. Next I will introduce some examples for you to introduce their usage.

1. js Property Operation Induction

We will encounter various operations in the front-end development process, which will involve operation attributes. Below I will tell you several common attribute operations in JS. Of course, I will not explain them using examples, it is just a brief introduction of usage.

1. Common attribute operations: obj. setAttribute (attribute name)

2. class operation: obj. className = ";

3. img src operation: imgobj. src = ";

4. input value operation: inputobj. value

5. Others

Ii. js attribute operations

1. special characters not allowed in JS

2. compatibility issues: js cannot be directly resolved through the obj. style. float attribute through IE (styleFloat) or non-IE (cssFloat ).


Iii. instance (set DIV attributes)

SetAttribute method:

The Code is as follows: Copy code

Var a = document. createElement ("div"); // create a new DIV
A. id = "div1"; // name the newly added DIV
A. style. setAttribute ("zIndex", 2); // sets the sequence of DIV stacked
A. style. setAttribute ("textAlign", Dalign); // alignment
A. style. setAttribute ("border", "# e6e7e8 1px solid"); // border color
A. style. width = divwidth; // DIV width
A. style. height = Dheight; // DIV height
A. setAttribute ("position", "absolute ");
A. style. backgroundColor = Dbgcolor; // DIV background
A. setAttribute ("z-index", "2"); // DIV stacked order
A. style. top = divtop + "px"; // DIV top margin
A. style. left = divleft + "px"; // left margin of the DIV
A. setAttribute ("innerHTML", info10 [0]. firstChild. data + "<br>" + info11 [0]. firstChild. data );
Document. body. appendChild (a); // The End of the newly created DIV.

Hide div: document. getElementById ("ah"). style. display = "none" // block appears

Document. getElementById ("ah"). style. disabled = "true"

Document. getElementById ("ah"). style. readOnly = "true"

Summary based on the above example

1: You can use (var attrName in obj) to traverse object attributes.

2: You can use attrName in obj to determine whether an object has a certain attribute. If this attribute is not available, undefined is returned. An object can add an attribute to an object, but it does not set a value for this attribute.

3: Determine whether a property of an object is defined in its own constructor rather than inherited. You can use the obj. hasOwnProperty (attrName) function ).

4: You can determine whether an attribute can be traversed by means of for/in. You can use obj. propertyInEnumerable (attrName), but this attribute must be obtained through non-inheritance.

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.