JavaScript对象反射用法实例
本文实例讲述了JavaScript对象反射用法。分享给大家供大家参考。具体如下:
这里讲述JavaScript对象反射用法,涉及反射DOM对象和自定义对象
<html>
<head>
<title>JavaScript反射工具</title>
<styletype="text/css">
#show{
width:400px;height:300px;
border:redsolid1px;
overflow:scroll;
}
#main{
width:500px;
text-align:left;
margin-left:auto;
margin-right:auto;
}
</style>
<scripttype='text/javascript'>
//生成选择的反射对象并反射
functionSwitchObj(){
varobj;
varswitchobj=document.getElementById('selects');
if(switchobj.value=="op_div"){
obj=document.createElement("div");
}
if(switchobj.value=="op_select"){
obj=document.createElement("select");
}
if(switchobj.value=="op_p"){
obj=document.createElement("p");
}
if(switchobj.value=="op_span"){
obj=document.createElement("span");
}
if(switchobj.value=="op_table"){
obj=document.createElement("table");
}
if(switchobj.value=="op_tr"){
obj=document.createElement("table");
}
if(switchobj.value=="op_window"){
obj=document.createElement("window");
}
if(switchobj.value=="op_document"){
obj=document.createElement("document");
}
Assembly(obj);
}
//反射对象
functionAssembly(obj){
varorder=0;
if(obj){
varassstr="反射对象:"+obj.tagName+"<br/>"
for(keyinobj){
order++;
assstr+=order+"----"+key+"<br/>";
}
Show(assstr);
}
}
//将反射信息输出
functionShow(msg){
varshowobj=document.getElementById('show');
if(showobj){
showobj.innerHTML="";
showobj.innerHTML=msg;
}
}
</script>
</head>
<body>
<divid="main">
<h1>JavaScript反射工具</h1>
<divid="show"></div>
<inputtype="button"id="btn_assembly"value="反射"
onclick="SwitchObj('select');"/>
<selectid="selects">
<optionvalue='op_div'>div</option>
<optionvalue='op_p'>p</option>
<optionvalue='op_span'>span</option>
<optionvalue='op_table'>table</option>
<optionvalue='op_select'>select</option>
<optionvalue='op_document'>document</option>
<optionvalue='op_window'>window</option>
</select>
</div>
</body>
</html>
希望本文所述对大家的javascript程序设计有所帮助。
热门推荐
10 祝孙辈生日祝福语简短
11 恋人跨年祝福语大全简短
12 考大学敬酒简短祝福语
13 开工投产大吉祝福语 简短
14 偶然结婚祝福语朋友简短
15 感谢学长学姐祝福语简短
16 戏精的祝福语简短
17 我要结婚祝福语简短
18 婚前女儿生日祝福语简短