JS Delete attribute and add attribute detailed

Source: Internet
Author: User
Tags border color

A. js attribute operation summary

We are in the front-end development process will encounter a variety of operations, which will involve the operation of properties, I will give you a few of the most common JS properties of the operation, of course, I will not take the example to explain, just briefly said usage.

1. Common attribute operation: Obj.setattribute (name of property)

2.class operation: Obj.classname= "";

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

4.input value operation: Inputobj.value

5. Other

Two. js attribute operation should pay attention to the problem

Special characters not allowed in 1.JS

2 Several compatibility issues: JS can not directly through the obj.style.float properties, through IE (stylefloat), non-IE (cssfloat) solution


three, instance (set Div's properties)

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); Set DIV Stacking order
A.style.setattribute ("TextAlign", dalign); Alignment method
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 stacking Order
A.style.top = divtop+ "px"; Div Top Margin
A.style.left = divleft+ "px"; Div left margin
A.setattribute ("InnerHTML", info10[0].firstchild.data+ "<br>" +info11[0].firstchild.data);
Document.body.appendChild (a); New Div End

Hide Div:document.getElementById ("Ah"). style.display= "None"//block appears

document.getElementById ("Ah"). Style.disabled= "true"

document.getElementById ("Ah"). Style.readonly= "true"

According to the above example summary

1: Traversal of object properties can be traversed by (Var attrname in obj)

2: To determine whether an object has a property that can be judged by attrname in obj, if no this property returns undefined, an object can add an attribute to an object, but does not set a value for this property

3: To determine whether a property of an object is defined in its own constructor rather than inherited, it can be obj.hasownproperty by function (attrname).

4: Determine if a property can be traversed through the for/in way through Obj.propertyinenumerable (Attrname), but the attribute must be derived from a way that is not inherited.

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.