In Ajax programming, it is often necessary to dynamically change the style of interface elements.

Source: Internet
Author: User

In Ajax programming, it is often necessary to dynamically change the style of interface elements, which can be changed through the style attribute of the object.
For example, to change the background color to red, you can write as follows:
Element. style. backgroundcolor = "# ff0000 ";
The style object has many attributes. Basically, attributes in CSS can be used in JavaScript. Now
If a function receives parameters to specify the style of an interface element, You need to design the parameter implementation.
Obviously, one or several parameters cannot meet the requirements. The following is an implementation:
Function setstyle (_ style ){
// Obtain the interface object for style change
VaR element = getelement ();
Element. Style = _ style;
}
In this way, the entire style object is passed in as a parameter. The possible form of a style object is:
VaR style = {
Color: # ffffff,
Backgroundcolor: # ff0000,
Borderwidth: 2px
}
You can call the function as follows:
Setstyle (style );
Or directly write as follows:
Setstyle ({color: # ffffff, backgroundcolor: # ff0000, borderwidth: 2px });
This Code does not seem to have any problems, but in fact, the setstyle function uses the parameter _ style
When element. style is assigned a value, if the element already has a certain style, for example, it has been executed:
Element. style. Height = "20px ";
The _ style does not include the definition of height, so the height style of the element is lost, not the most
The initial result. To solve this problem, you can use the reflection mechanism to override the setstyle function:
Function setstyle (_ style ){
// Obtain the interface object for style change
VaR element = getelement ();
For (var p in _ style ){
Element. Style [p] = _ style [p];
}
}
Traverse every attribute of _ style in the program, get the attribute name, and then use the square brackets syntax
The value of the corresponding attribute in _ style is assigned. Therefore, only the specified style is changed in element, while
Other styles are not changed and the expected results are obtained.

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.