本文实例讲述了javascript中createElement的两种创建方式。分享给大家供大家参考。具体实现方法如下:
<html xmlns="http://'>百度</a>"); //注意这里链接的文本“百度”是不会显示出来的, //必须设置innerText或innerHTML link.innerText = "百度"; document.body.appendChild(link); } function CreateLabel() { var lbl = document.createElement("label"); lbl.setAttribute("for", "userName"); lbl.setAttribute("myAge", "12"); //可以设置自定义标示 lbl.innerText = "用户名:"; document.body.appendChild(lbl); } </script></head><body> <input type="button" value="动态创建按钮1" onclick="CreateButton1()" /> <input type="button" value="动态创建按钮2" onclick="CreateButton2()" /> <input type="button" value="动态创建链接" onclick="CreateLink()" /> <input type="button" value="动态创建Label" onclick="CreateLabel()" /> <input type="text" id="userName" value="李莫" /></body></html>希望本文所述对大家的javascript程序设计有所帮助。