日期:2014-05-16 浏览次数:20431 次
?
? ? 注意:FF和Chrome不支持createElement('button') ? 所以要改成input 然后设置type..
?
? ?eval('btn').value 等价于getElementById('btn').value (不过这不符合w3c标准) IE9 FF7 Chrome14
?
<html> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <script language='javascript'> //一个对象 function Student(name,age){ //alert(arguments.length) this.name=name; this.age=age; this.sayHello=function(){ alert(this.name+': i am '+this.age); } } //xiaoming _s1=new Student('小明',12); //此为继承关系 document.write("s1 instanceof Student :"+(_s1 instanceof Student)+"<br>"); //构造 document.write("s1 constructor :"+(_s1.constructor)+"<br>"); //遍历属性 for(var tribute in _s1){ document.write('_s1============ '+tribute+":"+_s1[tribute]+"<br>") }; function onLoad(){ var my = document.getElementById("myDiv"); var btn = document.createElement("input"); btn.type='button'; btn.value='Hello'; alert(btn); //appendChild方法: my.appendChild(btn); } //事件工具 兼容IE(attachEvent) var EventUtil = { //添加事件 addHandler: function (oElement, sEvent, fnHandler) { //alert(oElement); oElement.addEventListener ? oElement.addEventListener(sEvent, fnHandler, false) : oElement.attachEvent("on" + sEvent, fnHandler) }, //移除事件 removeHandler: function (oElement, sEvent, fnHandler) { oElement.removeEventListener ? oElement.removeEventListener(sEvent, fnHandler, false) : oElement.detachEvent("on" + sEvent, fnHandler) }, //load事件 addLoadHandler: function (fnHandler) { this.addHandler(window, "load", fnHandler) } }; //初始化load EventUtil.addLoadHandler(function () { var aBtn = document.getElementsByTagName("input"); //为第一个按钮添加绑定事件 EventUtil.addHandler(aBtn[0], "click", function () { //Hello alert(aBtn[1].value); aBtn[0].value = "我可以点击了"; //sayHello() _s1.sayHello(); //EventUtil.addHandler(aBtn[0], "click",_s1.sayHello()); }); //事件处理函数 function fnHandler () { alert("事件绑定成功!") } }) </script> </head> <body onLoad="onLoad()"> <input type='button' value='我现在没什么用' id='btn'> <div id="myDiv"> </div> </body> </html>