JQuery中的事件 (五.事件对象的属性)

来源:互联网 发布:qq软件管家最新版 编辑:程序博客网 时间:2024/05/14 04:35

1.event.type

              获取事件类型

 <script>$(function(){$("a").click(function(event) {  alert(event.type);//获取事件类型  return false;//阻止链接跳转});})  </script></head><body><a href='http://google.com'>click me .</a></body>

2.event.preventDefault()

             阻止默认行为

3.event.stopPropagation()

            阻止冒泡行为

4.event.target

            获取到触发事件的元素

 <script>$(function(){$("a").click(function(event) {  var tg = event.target;  //获取事件对象  alert( tg ) ;  alert( tg.href ) ;  alert( tg.title ) ;  return false;//阻止链接跳转});})  </script></head><body><a href='http://google.com' title='nihaoma'>click me .</a></body>
5.event.pageX和event.pageY

           获取到光标相对于页面的x坐标和y坐标

 <script>$(function(){$("a").click(function(event) {  alert("Current mouse position: " + event.pageX + ", " + event.pageY );//获取鼠标当前相对于页面的坐标  return false;//阻止链接跳转});})  </script></head><body><a href='http://google.com'>click me .</a></body>
6.event.which

            在鼠标单击事件中获取到鼠标的左中右键;在键盘中获取键盘的按键

            (1)获取鼠标

 <script>$(function(){$("a").mousedown(function(e){alert(e.which)  // 1 = 鼠标左键 ; 2 = 鼠标中键; 3 = 鼠标右键    return false;//阻止链接跳转})})  </script></head><body><a href='http://google.com'>click me .</a></body>
         (2)获取键盘

 <script>$(function(){$("input").keyup(function(e){alert(e.which);})})  </script></head><body><input /></body>

7.event.metaKey

              获取ctrl按键

 <script>$(function(){$("input").keyup(function(e){alert( e.metaKey +" "+e.ctrlKey );$(this).blur();})})  </script></head><body><input type="text" value="按住ctrl键,然后再点其他任何键" style="width:200px"/></body>


0 0