jQuery使用笔记——事件对象

xiaoxiao2021-02-28  100

给出””标签的内容

<body> <p>click me</p> </body>

验证事件对象的各个属性含义 currentTarget

$(function(){ $("div").on("click",function(e){ console.log($(e.currentTarget));//div 事件的监听者 console.log($(e.target));//p 事件的目标 console.log($(this));//div }) })

delegateTarget

$(function(){ $("div").on("click","p",function(e){ console.log($(this).html());//打印标签p的内容 console.log($(e.delegateTarget));//当前事件的委托者div的样式内容 ($(e.delegateTarget)).css("border","1px solid red");//为div添加红色的边框 }) })

新的””标签内容+样式内容

<style> #log{ width:300px; height:300px; background-color:red; } </style> <body> <div id="log"></div> </body>

type

$(function(){ $("#log").on("mousemove",function(e){ console.log("pagex="+e.pageX+"pagey="+e.pageY);//输出鼠标在div内距离边框的上边缘和左边缘的位置 alert(e.type);//弹出事件类型(mousemove) }) })

给出新的””标签内容

<body> <a href="http://www.baidu.com">baidu</a> </body>

preventDefault

$(function(){ $("a").click(function(e){ e.preventDefault();//阻止默认事件,即不会跳转到百度首页,只是弹出弹窗"haha" alert("haha"); }) })
转载请注明原文地址: https://www.6miu.com/read-81300.html

最新回复(0)