为了账号安全,请及时绑定邮箱和手机立即绑定
课程 \ jQuery基础(三)—事件篇

jQuery基础(三)—事件篇

7-2 jQuery自定义事件之triggerHandler事件
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title></title>
<style>
.left div,
.right div {
width: 500px;
height: 50px;
padding: 5px;
margin: 5px;
float: left;
border: 1px solid #ccc;
}
.left div {
background: #bbffaa;
}
.right div {
background: yellow;
}
</style>
<script src="https://www.imooc.com/static/lib/jquery/1.9.1/jquery.js"></script>
</head>
<body>
<h2>自定义事件triggerHandler</h2>
<div class="left">
<div id="accident">
<a>triggerHandler事件</a>
<input type="text">
</div>
<button>事件冒泡,触发浏览器默认聚焦行为</button><br><br>
<button>不会冒泡,不触发浏览器默认聚焦行为</button>
</div>
<script type="text/javascript">
//给input绑定一个聚焦事件
$("input").on("focus",function(event,title) {
$(this).val(title)
});
$("#accident").on("click",function() {
alert("trigger触发的事件会在 DOM 树中向上冒泡");
});
//trigger触发focus
$("button:first").click(function() {
$("a").trigger("click");
$("input").trigger("focus");
});
//triggerHandler触发focus
$("button:last").click(function() {
$("a").triggerHandler("click");
$("input").triggerHandler("focus","没有触发默认聚焦事件");
});
</script>
</body>
</html>
2021-08-25 查看完整代码
7-1 jQuery自定义事件之trigger事件
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title></title>
<style>
.left div,
.right div {
width: 500px;
height: 50px;
padding: 5px;
margin: 5px;
float: left;
border: 1px solid #ccc;
}
.left div {
background: #bbffaa;
}
.right div {
background: yellow;
}
</style>
<script src="https://www.imooc.com/static/lib/jquery/1.9.1/jquery.js"></script>
</head>
<body>
<h2>自定义事件trigger</h2>
<div class="left">
<div><span></span><span>0</span>点击次数</div>
<button>直接点击</button>
<button>通过自定义点击</button>
</div>
<script type="text/javascript">
//点击更新次数
$("button:first").click(function(event,bottonName) {
bottonName = bottonName || 'first';
update($("span:first"),$("span:last"),bottonName);
});
//通过自定义事件调用,更新次数
$("button:last").click(function() {
$("button:first").trigger('click','last');
});
function update(first,last,bottonName) {
first.text(bottonName);
var n = parseInt(last.text(), 10);
last.text(n + 1);
}
</script>
</body>
</html>
2021-08-25 查看完整代码
6-2 jQuery事件对象的属性和方法
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title></title>
<style>
.left div,
.right div {
width: 500px;
height: 100px;
padding: 5px;
margin: 5px;
float: left;
border: 1px solid #ccc;
}
.left div {
background: #bbffaa;
}
.right div {
background: yellow;
}
</style>
<script src="https://www.imooc.com/static/lib/jquery/1.9.1/jquery.js"></script>
</head>
<body>
<h3>事件对象的属性与方法</h3>
<div class="left">
<div id="content">
外层div元素
<br />
<span style="background: silver;">内层span元素</span>
<br /> 外层div元素
</div>
<br />
<div id="msg"></div>
</div>
<script type="text/javascript">
//为 <span> 元素绑定 click 事件
$("span").click(function() {
$("#msg").html($("#msg").html() + "<p>内层span元素被单击</p>");
});
//为 Id 为 content 的 <div> 元素绑定 click 事件
$("#content").click(function(event) {
$("#msg").html($("#msg").html() + "<p>外层div元素被单击</p>");
event.stopPropagation(); //阻止事件冒泡
});
//为 <body> 元素绑定 click 事件
$("body").click(function() {
$("#msg").html($("#msg").html() + "<p>body元素被单击</p>");
});
</script>
</body>
</html>
2021-08-25 查看完整代码
6-1 jQuery事件对象的作用
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title></title>
<style>
.left div,
.right div {
width: 500px;
height: 100px;
padding: 5px;
margin: 5px;
float: left;
border: 1px solid #ccc;
}
.left div {
background: #bbffaa;
}
.right div {
background: yellow;
}
</style>
<script src="https://www.imooc.com/static/lib/jquery/1.9.1/jquery.js"></script>
</head>
<body>
<h3>事件委托,通过事件对象区别触发元素</h3>
<div class="left">
<div class="aaron">
<ul>
<li>点击:触发一</li>
<li>点击:触发二</li>
<li>点击:触发三</li>
<li>点击:触发四</li>
</ul>
</div>
</div>
<script type="text/javascript">
//多事件绑定一
$("ul").on('click',function(e){
alert('触发的元素是内容是: ' + e.target.textContent)
})
</script>
</body>
</html>
2021-08-25 查看完整代码
5-3 卸载事件off()方法
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title></title>
<style>
.left div,
.right div {
width: 100%;
height: 50px;
padding: 5px;
margin: 5px;
float: left;
border: 1px solid #ccc;
}
.left div {
background: #bbffaa;
}
.right div {
background: yellow;
}
</style>
<script src="https://www.imooc.com/static/lib/jquery/1.9.1/jquery.js"></script>
</head>
<body>
<h2>删除事件</h2>
<h4>测试一</h4>
<div class="left">
on('mousedown mouseup')
<div class="aaron">点击触发</div>
</div>
<button>点击删除mousedown事件</button>
<script type="text/javascript">
var n = 0;
//绑定事件
$(".aaron:first").on('mousedown mouseup', function(e) {
$(this).text( '触发类型:' + (e.type) + ",次数" + ++n)
++n;
})
//删除事件
$("button:first").click(function() {
$(".aaron:first").off('mousedown')
})
</script>
<h4>测试一</h4>
<div class="left">
on('mousedown mouseup')
<div class="aaron">点击触发</div>
</div>
<button>点击销毁所有事件off</button>
<script type="text/javascript">
var n = 0;
//绑定事件
$(".aaron:last").on('mousedown mouseup', function(e) {
$(this).text( '触发类型:' + (e.type) + ",次数" + ++n)
++n;
})
//删除事件
$("button:last").click(function() {
$(".aaron:last").off()
})
</script>
</body>
</html>
2021-08-25 查看完整代码
5-2 on()的高级用法
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title></title>
<style>
.left div,
.right div {
width: 500px;
height: 50px;
padding: 5px;
margin: 5px;
float: left;
border: 1px solid #ccc;
}
.left div {
background: #bbffaa;
}
.right div {
background: yellow;
}
</style>
<script src="https://www.imooc.com/static/lib/jquery/1.9.1/jquery.js"></script>
</head>
<body>
<h2>on事件委托</h2>
<div class="left">
<div class="aaron">
<a>点击这里</a>
</div>
</div>
<script type="text/javascript">
//给body绑定一个click事件
//没有直接a元素绑定点击事件
//通过委托机制,点击a元素的时候,事件触发
$('body').on('click', 'a', function(e) {
alert(e.target.textContent)
})
</script>
</body>
</html>
2021-08-25 查看完整代码
5-1 on()的多事件绑定
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title></title>
<style>
.left div,
.right div {
width: 100%;
height: 50px;
padding: 5px;
margin: 5px;
float: left;
border: 1px solid #ccc;
}
.left div {
background: #bbffaa;
}

.right div {
background: yellow;
}
</style>
<script src="https://www.imooc.com/static/lib/jquery/1.9.1/jquery.js"></script>
</head>
<body>
<h2>on绑定多事件</h2>
<h4>测试一</h4>
<div class="left">
点击触发:on('click',fn)
<div id="test1"></div>
</div>
<script type="text/javascript">
//事件绑定一
$("#test1").on('click', function(e) {
$(this).text('触发事件:' + e.type)
})
</script>
<h4>测试二</h4>
<div class="left">
点击触发:on('mousedown mouseup')
<div id="test2"></div>
</div>
<script type="text/javascript">
//多事件绑定一
$("#test2").on('mousedown mouseup', function(e) {
$(this).text('触发事件:' + e.type)
})
</script>
<h4>测试三</h4>
<div class="right">
点击触发:on(mousedown:fn1,mouseup:fn2)
<div id="test3"></div>
</div>
<script type="text/javascript">
//多事件绑定二
$("#test3").on({
mousedown: function(e) {
$(this).text('触发事件:' + e.type)
},
mouseup: function(e) {
$(this).text('触发事件:' + e.type)
}
})
</script>
</body>
</html>
2021-08-25 查看完整代码
4-2 jQuery键盘事件之keypress()事件
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title></title>
<style>
.left div,
.right div {
width: 500px;
height: 50px;
padding: 5px;
margin: 5px;
float: left;
border: 1px solid #ccc;
}
.left div {
background: #bbffaa;
}
em{
font-weight: 900;
color: red;
}
</style>
<script src="https://www.imooc.com/static/lib/jquery/1.9.1/jquery.js"></script>
</head>
<body>
<h2>keypress()事件</h2>
<div class="left">
<div class="aaron">监听keypress输入:
<input class="target1" type="text" value="" /><br />
输入中文测试,无法显示:<em></em>
</div>
</div>
<script type="text/javascript">
//监听键盘按键
//获取输入的值
$('.target1').keypress(function(e) {
$("em").text(e.target.value)
});
</script>
</body>
</html>
2021-08-25 查看完整代码
4-1 jQuery键盘事件之keydown()与keyup()事件
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title></title>
<style>
.left div,
.right div {
width: 500px;
height: 50px;
padding: 5px;
margin: 5px;
float: left;
border: 1px solid #ccc;
}
.left div {
background: #bbffaa;
}
em{
font-weight: 900;
color: red;
}
</style>
<script src="https://www.imooc.com/static/lib/jquery/1.9.1/jquery.js"></script>
</head>
<body>
<h2>keydown()与keyup()事件</h2>
<div class="left">
<h4>测试一</h4>
<div class="aaron">监听keydown输入:
<input class="target1" type="text" value="" /><br />
按下显示输入的值:<em></em>
</div>
<h4>测试二</h4>
<div class="aaron">监听keyup输入:
<input class="target2" type="text" value="" /><br />
松手显示输入的值:<em></em>
</div>
</div>
<script type="text/javascript">
//监听键盘按键
//获取输入的值
$('.target1').keydown(function(e) {
$("em:first").text(e.target.value)
});
//监听键盘按键
//获取输入的值
$('.target2').keyup(function(e) {
$("em:last").text(e.target.value)
});
</script>
</body>
</html>
2021-08-25 查看完整代码
3-4 jQuery表单事件之submit事件
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title></title>
<style>
.left div,
.right div {
width: 500px;
height: 50px;
padding: 5px;
margin: 5px;
float: left;
border: 1px solid #ccc;
}
.left div {
background: #bbffaa;
}
.right div {
background: yellow;
}
select {
height: 100px;
}
</style>
<script src="https://www.imooc.com/static/lib/jquery/1.9.1/jquery.js"></script>
</head>
<body>
<h2>submit</h2>
<div class="left">
<div class="aaron">
<form id="target1" action="test.html">
回车键或者点击提交表单:
<input type="text" value="输入新的值" />
<input type="submit" value="Go" />
</form>
</div>
<div class="aaron">
<form id="target2" action="destination.html">
回车键或者点击提交表单,禁止浏览器默认跳转:
<input type="text" value="输入新的值" />
<input type="submit" value="Go" />
</form>
</div>
</div>
<script type="text/javascript">
//回车键或者点击提交表单
$('#target1').submit(function(e) {
alert('捕获提交表达动作,不阻止页面跳转')
});
//回车键或者点击提交表单,禁止浏览器默认跳转:
$('#target2').submit(function() {
alert('捕获提交表达动作,阻止页面跳转')
return false;
});
</script>
</body>
</html>
2021-08-25 查看完整代码
3-3 jQuery表单事件之select事件
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title></title>
<style>
.left div,
.right div {
width: 500px;
height: 80px;
padding: 5px;
margin: 5px;
float: left;
border: 1px solid #ccc;
}
.left div {
background: #bbffaa;
}
.right div {
background: yellow;
}
select {
height: 100px;
}
</style>
<script src="https://www.imooc.com/static/lib/jquery/1.9.1/jquery.js"></script>
</head>
<body>
<h2>input与textarea</h2>
<div class="left">
<h4>测试一</h4>
<div class="aaron">
选中文字:input
<input type="text" value="慕课网" />
</div>
<button id="bt1">触发input元素的select事件</button>

<h4>测试二</h4>
<div class="aaron">
textarea:
<textarea rows="3" cols="20">用鼠标选中文字</textarea>
</div>
</div>
<script type="text/javascript">
//监听input元素中value的选中
//触发元素的select事件
$("input").select(function(e){
alert(e.target.value)
})
$("#bt1").click(function(){
$("input").select();
})
//监听textarea元素中value的选中
$('textarea').select(function(e) {
alert(e.target.value);
});
</script>
</body>
</html>
2021-08-25 查看完整代码
3-2 jQuery表单事件之change事件
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title></title>
<style>
.left div,
.right div {
width: 100%;
padding: 5px;
margin: 5px;
float: left;
border: 1px solid #ccc;
}
.left div {
background: #bbffaa;
}

.right div {
background: yellow;
}
</style>
<script src="https://www.imooc.com/static/lib/jquery/1.9.1/jquery.js"></script>
</head>
<body>
<h2>input、textarea与select</h2>
<div class="left">
<div class="aaron">input:
<input class="target1" type="text" value="监听input的改变" />
</div>
<div class="aaron1">select:
<select class="target2">
<option value="option1" selected="selected">Option 1</option>
<option value="option2">Option 2</option>
</select>
</div>
<div class="aaron3">textarea:
<textarea class="target2" rows="3" cols="20">多行的文本输入控件</textarea>
</div>
</div>
输出结果:
<div id="result"></div>
<script type="text/javascript">
//监听input值的改变
$('.target1').change(function(e) {
$("#result").html(e.target.value)
});
//监听select:
$(".target2").change(function(e) {
$("#result").html(e.target.value)
})
//监听textarea:
$(".target3").change(function(e) {
$("#result").html(e.target.value)
})
</script>
</body>
</html>
2021-08-25 查看完整代码
3-1 jQuery表单事件之blur与focus事件
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title></title>
<style>
.left div,
.right div {
width: 500px;
height: 50px;
padding: 5px;
margin: 5px;
float: left;
border: 1px solid #ccc;
}
.left div {
background: #bbffaa;
}
.right div {
background: yellow;
}
</style>
<script src="https://www.imooc.com/static/lib/jquery/1.9.1/jquery.js"></script>
</head>
<body>
<h4>.focusin与blur</h4>
<div class="left">
<div class="aaron">
点击触发焦点(无反应):
<input type="text" />
</div>
<div class="aaron1">
点击触发焦点并冒泡:
<input type="text" />
</div>
</div>
<script type="text/javascript">
$(".aaron").focus(function() {
$(this).css('border', '2px solid red')
})
$(".aaron1").focusin(function() {
$(this).find('input').val('冒泡捕获了focusin事件')
})
</script>
<h4>.focusout与blur</h4>
<div class="right">
<div class="aaron3">
点击触发失去焦点(无反应):
<input type="text" />
</div>
<div class="aaron4">
点击触发失去焦点并冒泡:
<input type="text" />
</div>
</div>
<script type="text/javascript">
$(".aaron3").blur(function() {
$(this).css('border', '2px solid red')
})
$(".aaron4").focusout(function() {
$(this).find('input').val('冒泡捕获了focusout事件')
})
</script>
</body>
</html>
2021-08-25 查看完整代码
2-9 jQuery鼠标事件之focusout事件
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title></title>
<style>
.left div,
.right div {
width: 500px;
height: 50px;
padding: 5px;
margin: 5px;
float: left;
border: 1px solid #ccc;
}
.left div {
background: #bbffaa;
}
.right div {
background: yellow;
}
</style>
<script src="https://www.imooc.com/static/lib/jquery/1.9.1/jquery.js"></script>
</head>
<body>
<h2>.focusout()方法</h2>
<div class="left">
<div class="aaron">
点击触发失去焦点:<input type="text" />
</div>
</div>
<div class="right">
<div class="aaron1">
点击触发失去焦点并传递参数:<input type="text" />
</div>
</div>
<script type="text/javascript">
//input失去焦点
//给input元素增加一个边框
$("input:first").focusout(function() {
$(this).css('border','2px solid red')
})
</script>
<script type="text/javascript">
//不同函数传递数据
function fn(e) {
$(this).val(e.data)
}
function a() {
$("input:last").focusout('慕课网', fn)
}
a();
</script>
</body>
</html>
2021-08-25 查看完整代码
2-8 jQuery鼠标事件之focusin事件
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title></title>
<style>
.left div,
.right div {
width: 500px;
height: 50px;
padding: 5px;
margin: 5px;
float: left;
border: 1px solid #ccc;
}
.left div {
background: #bbffaa;
}
.right div {
background: yellow;
}
</style>
<script src="https://www.imooc.com/static/lib/jquery/1.9.1/jquery.js"></script>
</head>
<body>
<h2>.focusin()方法</h2>
<div class="left">
<div class="aaron">
点击聚焦:<input type="text" />
</div>
</div>
<div class="right">
<div class="aaron1">
点击聚焦并传递参数:<input type="text" />
</div>
</div>
<script type="text/javascript">
//input聚焦
//给input元素增加一个边框
$("input:first").focusin(function() {
$(this).css('border','2px solid red')
})

</script>
<script type="text/javascript">
//不同函数传递数据
function fn(e) {
$(this).val(e.data)
}
function a() {
$("input:last").focusin('慕课网', fn)
}
a();
</script>
</body>
</html>
2021-08-24 查看完整代码
2-7 jQuery鼠标事件之hover事件
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title></title>
<style>
.left div,
.right div {
width: 350px;
height: 150px;
padding: 5px;
margin: 5px;
border: 1px solid #ccc;
}
p {
height: 50px;
border: 1px solid red;
margin: 30px;
}
.left div {
background: #bbffaa;
}
</style>
<script src="https://www.imooc.com/static/lib/jquery/1.9.1/jquery.js"></script>
</head>
<body>
<h2>.hover()方法</h2>
<div class="left">
<div class="aaron1">
<p>触发hover事件</p>
</div>
</div>
<script type="text/javascript">
// hover()方法是同时绑定 mouseenter和 mouseleave事件。
// 我们可以用它来简单地应用在 鼠标在元素上行为
$("p").hover(
function() {
$(this).css("background", 'red');
},
function() {
$(this).css("background", '#bbffaa');
}
);
</script>
</body>
</html>
2021-08-24 查看完整代码
2-5 jQuery鼠标事件之mouseenter与mouseleave事件
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title></title>
<style>
.left div,
.right div {
width: 350px;
height: 150px;
padding: 5px;
margin: 5px;
border: 1px solid #ccc;
}
p{
height: 50px;
border: 1px solid red;
margin: 30px;
}
.left div {
background: #bbffaa;
}
.right div {
background: yellow;
}
</style>
<script src="https://www.imooc.com/static/lib/jquery/1.9.1/jquery.js"></script>
</head>
<body>
<h2>.mouseover()方法</h2>
<div class="left">
<div class="aaron1">
<p>鼠标离开此区域触发mouseover事件</p>
<a>mouseover事件触发次数:</a><br/>
<a>mouseover冒泡事件触发次数:</a>
</div>
</div>
<h2>.mouseenter()方法</h2>
<div class="right">
<div class="aaron2">
<p>鼠标进入此区域触发mouseenter事件</p>
<a>mouseenter事件触发次数:</a><br/>
<a>mouseenter冒泡事件触发次数:</a>
</div>
</div>
<br/>
<script type="text/javascript">
var i = 0;
$(".aaron1 p").mouseover(function(e) {
$(".aaron1 a:first").html('mouseover事件触发次数:' + (++i))
})
var n = 0;
$(".aaron1").mouseover(function() {
$(".aaron1 a:last").html('mouseover冒泡事件触发次数:' + (++n))
})
</script>
<script type="text/javascript">
var i = 0;
$(".aaron2 p").mouseenter(function(e) {
$(".aaron2 a:first").html('mouseenter事件触发次数:' + (++i))
})
var n = 0;
$(".aaron2").mouseenter(function() {
$(".aaron2 a:last").html('mouseenter冒泡事件触发次数:' + (++n))
})
</script>
</body>
</html>
2021-08-24 查看完整代码
2-4 jQuery鼠标事件之mouseover与mouseout事件
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title></title>
<style>
.left div,
.right div {
width: 350px;
height: 150px;
padding: 5px;
margin: 5px;
border: 1px solid #ccc;
}
p{
height: 50px;
border: 1px solid red;
margin: 30px;
}
.left div {
background: #bbffaa;
}
.right div {
background: yellow;
}
</style>
<script src="https://www.imooc.com/static/lib/jquery/1.9.1/jquery.js"></script>
</head>
<body>
<h2>mouseover与mouseout事件</h2>
<h4>测试一</h4>
<button>点击:指定触发mouseover事件</button>
<script type="text/javascript">
$('h2').mouseover(function(e) {
alert('触发h2元素绑定的mouseover')
})
$("button:eq(0)").click(function(e) {
$('h2').mouseover() //指定触发绑定的事件
})
</script>
<h4>测试二</h4>
<div class="left">
<div class="aaron1">
<p>鼠标移进此区域触发mouseover事件</p>
<a>进入元素内部,mouseover事件触发次数:</a>
</div>
</div>
<script type="text/javascript">
var n = 0;
//绑定一个mouseover事件
$(".aaron1 p:first").mouseover(function(e) {
$(".aaron1 a").html('进入元素内部,mouseover事件触发次数:' + (++n))
})
</script>
<h4>测试三</h4>
<div class="right">
<div class="aaron2">
<p>鼠标移动:不同函数传递数据</p>
<a>进入元素内部,mouseover事件触发次数:</a>
</div>
</div>
<br/>
<script type="text/javascript">
var n = 0;
//不同函数传递数据
function data(e) {
$(".right a").html('mouseover事件触发次数:' + (++n) + '<br/> 传入数据为 :'+ e.data)
}

function a() {
$(".right p:first").mouseover('data = 慕课网', data)
}
a();
</script>
</body>
</html>
2021-08-24 查看完整代码
2-3 jQuery鼠标事件之mousemove事件
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title></title>
<style>
.left div,
.right div {
width: 300px;
height: 80px;
padding: 5px;
margin: 5px;
border: 1px solid #ccc;
}
.left div {
background: #bbffaa;
}

.right div {
background: yellow;
}
</style>
<script src="https://www.imooc.com/static/lib/jquery/1.9.1/jquery.js"></script>
</head>
<body>
<h2>.mousemove()方法</h2>
<h4>测试一</h4>
<button>点击:指定触发mousemove事件</button>
<script type="text/javascript">
$('h2').mousemove(function(e) {
alert('触发h2元素绑定的mousemove')
})

$("button:eq(0)").click(function(e) {
$('h2').mousemove() //指定触发绑定的事件
})
</script>
<h4>测试二</h4>
<div class="left">
<div class="aaron1">
<p>鼠标在绿色区域移动触发mousemove</p>
<p>移动的X位置:</p>
</div>
</div>
<script type="text/javascript">
//绑定一个mousemove事件
//触发后修改内容
$(".aaron1").mousemove(function(e) {
$(this).find('p:last').html('移动的X位置:' + e.pageX)
})
</script>
<h4>测试三</h4>
<div class="right">
<div class="aaron3">
<p>鼠标移动:不同函数传递数据</p>
<p>数据:</p>
</div>
</div>
<script type="text/javascript">
//不同函数传递数据
function data(e) {
$(this).find('p:last').html('数据:' + e.data)
}
function a() {
$(".right").mousemove(1111, data)
}
a();
</script>
</body>
</html>
2021-08-24 查看完整代码
2-2 jQuery鼠标事件之mousedown与mouseup事件
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title></title>
<style>
.test2 {
background: #bbffaa;
}
.test3 {
background: yellow;
}
.test2,
.test3 {
border: 1px solid red;
}
</style>
</style>
<script src="https://www.imooc.com/static/lib/jquery/1.9.1/jquery.js"></script>
</head>
<body>
<h2>.mousedown()方法</h2>
<h4>测试一</h4>
<button>弹出回调中的鼠标键</button>
<script type="text/javascript">
//this指向button元素
$("button:eq(0)").?(function(e) {
alert('e.which: ' + e.which)
})
</script>
<h4>测试二</h4>
<div class="test2">
<p>$('button:first').mousedown(function(e) {alert(this)})</p>
</div>
<button>指定触发事件</button>
<script type="text/javascript">
$('p').mousedown(function(e) {
alert(e.target.textContent)
})
//this指向button元素
$("button:eq(1)").mousedown(function() {
$('p').mousedown() //指定触发绑定的事件
})
</script>
<h4>测试三</h4>
<div class="test3">
<p>$('.right').mousedown(1111, set)</p>
</div>
<button>不同函数传递数据</button>
<script type="text/javascript">
//不同函数传递数据
function data(e) {
alert(e.data) //1111
}
function a() {
$("button:eq(2)").mousedown(1111, data)
}
a();
</script>
</body>
</html>
2021-08-23 查看完整代码
首页上一页12下一页尾页
意见反馈 帮助中心 APP下载
官方微信