JavaScript對象反射用法執行個體_javascript技巧

來源:互聯網
上載者:User

本文執行個體講述了JavaScript對象反射用法。分享給大家供大家參考。具體如下:

這裡講述JavaScript對象反射用法,涉及反射DOM對象和自訂對象

<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>

希望本文所述對大家的javascript程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.