Javascript example for...in loop:
<html><head><title>A Javascript example using a for...in loop</title></head><body><script type="text/javascript">// Create an object myObject and three properties sitename, siteurl, sitecontent. var myObject = new Object();myObject.sitename = "Blabula";myObject.siteurl = "blabla.cn";myObject.sitecontent = "Chinese site of web tutorial code gallery";//Tranquility through all properties of the object for (prop in myObject){document.write("prop'" + prop + "' is " + myObject[prop]);document.write("<br>");}</script></body></html>Today, the Java Tang blog online found a method to iterate through all the attribute names and values of a JavaScript object. This is very intuitive and convenient when using the method. The code is as follows:
/* * Used to traverse all attribute names and values of the specified object* obj The object that needs to be traversed* author: Jet Mah */ function allPrpos ( obj ) { // Used to save all attribute names and values var props = "" ; // Start traversal for ( var p in obj ){ // Method if ( typeof ( obj [ p ]) == " function " ){ obj [ p ]() ; } else { // p is the attribute name, obj[p] is the value of the corresponding attribute props += p + " = " + obj [ p ] + " /t " ; } } // Finally, all attributes alert ( props ) ; }AJAX's JavaScript reflection mechanism refers to the program being able to obtain its own information when it is run. For example, an object can know what methods and properties it has at runtime. In JavaScript, the syntax is as follows:
for(var p in obj){ //Statement}In Ajax programming, it is often necessary to dynamically change the style of interface elements. This can be changed through the object's style attribute. For example, to change the background color to red, you can write it like this:
element.style.backgroundColor="#ff0000";
Basically, the properties that are owned in CSS can be used in JavaScript:
function setStyle(_style){ //Get the interface object to change the style var element=getElement(); element.style=_style; }The entire style object is directly passed as a parameter:
var style={ color:#ffffff, backgroundColor:#ff0000, borderWidth:2px }You can call the function like this:
setStyle(style);
Or write it directly as:
setStyle({ color:#ffffff,backgroundColor:#ff0000,borderWidth:2px});
This code seems to have no problem, but in fact, when using the parameter _style to element.style inside the setStyle function, if the element already has a certain style, for example, it has been executed:
element.style.height="20px";
However, the definition of height is not included in _style, so the height style of element is lost, not the initial result. To solve this problem, you can use the reflection mechanism to override the setStyle function:
function setStyle(_style){ //Get the interface object to change the style var element=getElement(); for(var p in _style){ element.style[p]=_style[p]; } }Iterate through each attribute of _style in the program to get the attribute name, and then use square bracket syntax to assign the corresponding attribute in element.style to the corresponding attribute in _style.
The above article on the JS traversal page is all the content shared by the editor. I hope it can give you a reference and I hope you can support Wulin.com more.