Posted in Javascript onSeptember 16, 2013
这一期介绍一些简单的事件处理:
1.鼠标点击
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title> New Document </title> <meta name="Generator" content="EditPlus"> <meta name="Author" content=""> <meta name="Keywords" content=""> <meta name="Description" content=""> <script type="text/javascript"> function click1(obj) { alert(obj.innerHTML); } </script> </head> <body> <div onclick="click1(this)" style="cursor:pointer">点击我吧</div> </body> </html>
this即div的上下文,点击后会弹出<div>之间的内容。
2.鼠标移动
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title> New Document </title> <meta name="Generator" content="EditPlus"> <meta name="Author" content=""> <meta name="Keywords" content=""> <meta name="Description" content=""> <script type="text/javascript"> function mouse1(obj) { obj.style.color = "#f00"; obj.style.fontSize = "18px" } function mouse2(obj) { obj.style.color = "#000"; obj.style.fontSize = "16px" } </script> </head> <body> <div onmouseover="mouse1(this)" onmouseout="mouse2(this)">移动到这儿</div> </body> </html>
onmouseover鼠标放到那儿时,会把字体变大,字体成为红色,离开时字体和颜色恢复。
3.with用法
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title> New Document </title> <meta name="Generator" content="EditPlus"> <meta name="Author" content=""> <meta name="Keywords" content=""> <meta name="Description" content=""> <script type="text/javascript"> with(document) { write("niujiabin"+"<br/>"); write("maybe"+"<br/>"); write("gossip"+"<br/>"); } </script> </head> <body> </body> </html>
效果与如下相同
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title> New Document </title> <meta name="Generator" content="EditPlus"> <meta name="Author" content=""> <meta name="Keywords" content=""> <meta name="Description" content=""> <script type="text/javascript"> document.write("niujiabin"+"<br/>"); document.write("maybe"+"<br/>"); document.write("gossip"+"<br/>"); </script> </head> <body> </body> </html>
javascript简单事件处理和with用法介绍
声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@