u jayakodi
12-16-2010, 06:38 AM
jayakodiu@yahoo.com
DIV tag mimic for CLASS object
A DIV tag does not show anything unless the background color and width or height are defined; this makes it an ideal candidate to be an invisible 'container' to hold info by setting attributes of our liking. The following code for a DIV tag mimics a CLASS object, which otherwise is quite complex:
<SCRIPT>
//The function to create the rectangle class and properties
function addrec(sx,sy,w,h){
var ele=document.body.appendChild(document.createElement('DIV'))
//add properties to our rectangle object
ele.setAttribute('name','rectangle')
ele.setAttribute('sx',sx)
ele.setAttribute('sy',sy)
ele.setAttribute('w',w)
ele.setAttribute('h',h)
area=w*h
ele.setAttribute('area',area)
perimeter=2*(w+h)
ele.setAttribute('peri',perimeter)
momtofinertia=w*h*h*h/12
ele.setAttribute('momin',momtofinertia)
}
//test the class creating 5 rectangles
addrec(100,100,10,20)
addrec(100,100,20,20)
addrec(100,100,30,20)
addrec(100,100,40,20)
addrec(100,100,50,20)
var rn=document.all.tags("DIV")
n=rn.length
for(i=0;i<n;i++){nam=rn[i].getAttribute('name')
if(nam=='rectangle'){alert(rn[i].momin)}
}
//we can have an attribute 'index' or have a parent DIV to hold our objects as children.
</SCRIPT>
DIV tag mimic for CLASS object
A DIV tag does not show anything unless the background color and width or height are defined; this makes it an ideal candidate to be an invisible 'container' to hold info by setting attributes of our liking. The following code for a DIV tag mimics a CLASS object, which otherwise is quite complex:
<SCRIPT>
//The function to create the rectangle class and properties
function addrec(sx,sy,w,h){
var ele=document.body.appendChild(document.createElement('DIV'))
//add properties to our rectangle object
ele.setAttribute('name','rectangle')
ele.setAttribute('sx',sx)
ele.setAttribute('sy',sy)
ele.setAttribute('w',w)
ele.setAttribute('h',h)
area=w*h
ele.setAttribute('area',area)
perimeter=2*(w+h)
ele.setAttribute('peri',perimeter)
momtofinertia=w*h*h*h/12
ele.setAttribute('momin',momtofinertia)
}
//test the class creating 5 rectangles
addrec(100,100,10,20)
addrec(100,100,20,20)
addrec(100,100,30,20)
addrec(100,100,40,20)
addrec(100,100,50,20)
var rn=document.all.tags("DIV")
n=rn.length
for(i=0;i<n;i++){nam=rn[i].getAttribute('name')
if(nam=='rectangle'){alert(rn[i].momin)}
}
//we can have an attribute 'index' or have a parent DIV to hold our objects as children.
</SCRIPT>