Home  >  Article  >  Web Front-end  >  JavaScript object reflection usage examples_javascript skills

JavaScript object reflection usage examples_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:03:241705browse

The examples in this article describe the usage of JavaScript object reflection. Share it with everyone for your reference. The details are as follows:

Here we describe the usage of JavaScript object reflection, involving reflective DOM objects and custom objects

<html>
<head>
<title>JavaScript反射工具</title>
<style type="text/css">
#show{
width:400px;height:300px;
border:red solid 1px;
overflow:scroll;
}
#main{
width:500px;
text-align:left;
margin-left:auto;
margin-right:auto;
}
</style>
<script type='text/javascript'>
//生成选择的反射对象并反射
function SwitchObj(){
 var obj;
 var switchobj=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);
}
//反射对象
function Assembly(obj){
  var order=0;
  if(obj){
    var assstr="反射对象:"+obj.tagName+"<br/>"
    for(key in obj){
      order++;
      assstr+=order+"----"+key+"<br/>";
    }
    Show(assstr);
  }
}
//将反射信息输出
function Show(msg){
  var showobj=document.getElementById('show');
  if(showobj){
    showobj.innerHTML="";
    showobj.innerHTML=msg;
   }
}
</script>
</head>
<body>
<div id="main">
<h1>JavaScript反射工具</h1>
<div id="show"></div>
<input type="button" id="btn_assembly" value="反射" 
onclick="SwitchObj('select');" />
<select id="selects">
<option value='op_div'>div</option>
<option value='op_p'>p</option>
<option value='op_span'>span</option>
<option value='op_table'>table</option>
<option value='op_select'>select</option>
<option value='op_document'>document</option>
<option value='op_window'>window</option>
</select>
</div>
</body>
</html>

I hope this article will be helpful to everyone’s JavaScript programming design.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn