语法:
oElement = document . createElement ( sTagName )
参数:
sTagName : 必选项。字符串(String)。
返回值:
oElement : 对象(Element)。返回新建对象的引用。
说明:
根据指定的标签建立新的对象实例。
在IE4.0上仅仅可以用于新建 img , area , option 。在IE5.0上你可以用于新建除了 frame 和 iframe 以外的所有对象类型。
新建对象的属性是可读取以及被程序控制的。
在使用本方法新建对象后,你必须明确的将对象置入集合或文档层次结构中,方可使用它们。
对于 input 而言,因其默认的 type 属性值为 text ,所以新建的实例默认为文本输入框。若期望其他种类的 input ,则使用本方法建立对象时,要设置其 type 属性。
sTagName 可以包含属性,只要整个字符串(String)是有效的HTML。如果你希望在运行时用本方法建立的对象包含 name 属性,你应该这样做。
示例:
<script>
function rdl_fnCreate(){
oData.innerHTML="";
with (oSel) var oOption=options[selectedIndex];
if(oOption.text.length<1) return;
var aElement=document.createElement(oOption.text);
eval("aElement."+oOption.value+"='"+oText.value+"'");
oView.innerText="document.createElement('"+oOption.text+"');"
if(oOption.text=="A") {aElement.href="#"; aElement.onclick=new Function("return false;");}
if(oOption.text=="<FORM>") {aElement.border="1px solid #CCCCCC";var oReset=document.createElement("<input type=reset id=shit>");aElement.appendChild(oReset);}
oData.appendChild(aElement);
window.resizeTo(360,260);
}
</script>
<div id="oData"></div><br>
<table width=98%><tr><td>
<select id="oSel">
<option value="innerText">A
<option value="value"><INPUT TYPE="button">
<option value="innerText"><FORM>
</select>
</td><td>
<input type="text" id="oText" value="示范文字" size=12>
</td><td>
<input type=button value=" 建立 " onclick= "rdl_fnCreate();">
</td></tr></table><br>
<div id=oView style="font-size:10px;line-height:16px;background:#DFDFDF;padding:8px;"></div>
