课件中给创建的元素节点用了nodename.属性=“值”和node.setAttribute("属性","值")。在第二案例代码里,对于type、name、value二者可通用不影响结果。但对于onclick却不能这样btn.onclick="javascript:alert('This is a text!')"
是因为onclick不属于属性?
是因为onclick不属于属性?
2015-05-17
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>无标题文档</title> </head> <body> <script type="text/javascript"> var main = document.body; //创建链接 function createa(url,text) { var a=document.createElement("a"); a.innerHTML=text; a.href=url a.style.color="red"; main.appendChild(a); // window.open("http://www.imooc.com","_blank") } // 调用函数创建链接 var btn = document.createElement("input"); btn.type="button"; btn.value="显示慕课网链接"; // var url="http://www.imooc.com"; //text="慕课网"; //btn.onclick=createa(url,text); btn.onclick=createa('http://www.imooc.com','慕课网'); // btn.setAttribute("onclick", "createa('http://www.imooc.com','慕课网')"); btn.style.backgroundColor="red"; main.appendChild(btn); </script> </body> </html>
举报