标签:
1 代码: 2 <input type="text" id="name" /> 3 4 window.onload = function () { 5 document.getElementById("name").onkeypress = function (event) { 6 if (event.keyCode >115) 7 { 8 return false; 9 } 10 } 11 }
1 <form> 2 <%-- this代表事件发生的目标对象 --%> 3 <input id="b1" type="button" value="Button1" onclick="alert(this.form.b2.value);" /> 4 <%-- this代表事件发生的目标对象,所以可以省略this --%> 5 <input id="b2" type="button" value="Button2" onclick="alert(form.b1.value);" /> 6 <%--form在此匿名函数的定义域(document) 当中,所以可以省略form --%> 7 <input id="b3" type="button" value="Button3" onclick="alert(b4.value);" /> 8 <%--此匿名函数的定义的作用域为window.document当中,所以可以省略document --%> 9 <input id="b4" type="button" value="Button4" onclick="alert(getElementById(‘b3‘).value);" /> 10 <%--此匿名函数的定义的作用域为window.document当中,但document对象也是运行在window当中,所以可以省略window --%> 11 </form>
1 var b4 = document.getElementById("b4"); 2 b4.onclick = function () { 3 with (document) { 4 with (this.form) { 5 with (this) { 6 alert(b3.value); 7 } 8 } 9 } 10 }
1 <div id="div1" style="height:100px;height:40px;border:solid 1px red;"> 2 <input id="b1" type="button" value="Button1" /> 3 </div>
1 window.onload = function () { 2 var b1 = document.getElementById("b1"); 3 b1.addEventListener("click", function () { 4 alert("this is b1"); 5 }) 6 var div1 = document.getElementById("div1"); 7 div1.addEventListener("click", function () { 8 alert("this is div1"); 9 }, true) 10 11 }
1 function mousemoveFun(event) { 2 var left = event.clientX - div1.style.width.substring(0, div1.style.width.length - 2) / 2; 3 var top = event.clientY - div1.style.height.substring(0, div1.style.height.length - 2) / 2; 4 div1.style.left = left + "px"; 5 div1.style.top = top + "px"; 6 } 7 window.onload = function () { 8 var b1 = document.getElementById("b1"); 9 var div1 = document.getElementById("div1"); 10 var ismove = false; 11 div1.addEventListener("mousedown", function (event) { 12 document.addEventListener("mousemove", mousemoveFun, true); 13 ismove = true; 14 }) 15 document.addEventListener("mouseup", function (event) { 16 if (!ismove) { 17 return; 18 } 19 ismove = false; 20 document.removeEventListener("mousemove", mousemoveFun, true); 21 var left = event.clientX - div1.style.width.substring(0, div1.style.width.length - 2) / 2; 22 var top = event.clientY - div1.style.height.substring(0, div1.style.height.length - 2) / 2; 23 div1.style.left = left + "px"; 24 div1.style.top = top + "px"; 25 }, true); 26 //注意:mousemove和mouseup事件的句柄被注册为捕捉事件句柄,因为用户移动鼠标的速度比跟跟随它移动的文档元素快,所以其中一些事件发生在 27 //原始目标元素外部,没有捕获,事件可能无法分配给正确的句柄 28 }
1 <input id="b1" type="button" value="Button1" /> 2 var o = new Object(); 3 o.value = "js对象"; 4 o.func = function (event) { 5 //this引用的 o 而不是文档元素对象 6 alert(this.value); 7 } 8 window.onload = function () { 9 var b1 = document.getElementById("b1"); 10 b1.addEventListener("click", function (event) { 11 o.func(event); 12 }, true); 13 }
1 <input id="b1" type="button" value="Button1" /> 2 var e; 3 window.onload = function () { 4 var b1 = document.getElementById("b1"); 5 b1.addEventListener("click", function (event) { 6 dataEvent.receive("b1", e, function (event) { 7 alert(event.datatype + ":" + event.data); 8 }); 9 }, true); 10 var e = dataEvent.send("b1", "name", "小三"); 11 } 12 var dataEvent = {}; 13 dataEvent.send = function (target, datatype, data) { 14 if (typeof target == "string") { 15 target = document.getElementById(target); 16 } 17 if (document.createEvent) { 18 var e = document.createEvent("Events");//此处使用的是接口名 Events UIEvents MouseEvents MutationEvents 有可能没有s 19 e.initEvent("dataavailable", true, false);//初始化自定义事件 20 } 21 else if (document.createEventObject) { 22 var e = document.createEventObject(); 23 } 24 else return; 25 //自定义事件的一些属性 26 e.datatype = datatype; 27 e.data = data; 28 return e; 29 } 30 dataEvent.receive = function (target, event, handler) { 31 if (typeof target == "string") 32 { target = document.getElementById(target); } 33 if (target.addEventListener) 34 { target.addEventListener("dataavailable", handler, false); } 35 else if (target.attachEvent) { 36 target.attachEvent("ondataavailable", handler); 37 } 38 if (target.dispatchEvent) { 39 target.dispatchEvent(event); 40 } 41 else if (target.fireEvent) { 42 target.fireEvent("ondataavailable", event); 43 } 44 }
JavaScript 客户端JavaScript之事件(DOM API 提供模块之一)
标签:
原文地址:http://www.cnblogs.com/tlxxm/p/4375023.html