一、on(events,[selector],[data],fn)
在选择元素上绑定一个或多个事件的事件处理函数。
$("p").on("click", function(){
alert( $(this).text() );
});
二、bind(type,[data],fn)
为每个匹配元素的特定事件绑定事件处理函数。
当每个段落被点击的时候,弹出其文本。
jQuery 代码:
$("p").bind("click", function(){
alert( $(this).text() );
});
三、trigger(type,[data])
在每一个匹配的元素上触发某类事件。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>音乐导航</title>
<style type="text/css">
*{
margin: 0;
padding: 0;
list-style: none;
}
.nav {
width: 900px;
height: 60px;
background: black;
margin: 0 auto;
}
.nav li{
width: 100px;
height: 60px;
float: left;
position: relative;
overflow: hidden;
}
.nav a{
position: absolute;
width: 100%;
height: 100%;
font-size: 24px;
color: seagreen;
text-decoration: none;
text-align: center;
line-height: 60px;
z-index: 2;
}
.nav span {
position: absolute;
width: 100%;
height: 100%;
background: tan;
top: 60px;
}
</style>
<script src="js/jquery-3.3.1.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
// $(function(){
// $(".nav li").mouseenter(function(){
// $(this).children("span").stop().animate({top:0});
// var index=$(this).index();
// $("audio").get(index).load();
// $("audio").get(index).play();
// });
// $(".nav li").mouseleave(function(){
// $(this).children("span").stop().animate({ top: 60 });
// });
// });
//
// var flag=true;
// $(document).on("keydown",function(e){
// if (flag) {
// flag=false;
// var code=e.keyCode;
// if(code>=49 && code<=57){
// $(".nav li").eq(code-49).trigger("mouseenter");
// }
// }
// });
//
// $(document).on("keyup",function(e){
// flag=true;
// var code=e.keyCode;
// if(code>=49 && code<=57){
// $(".nav li").eq(code-49).trigger("mouseleave");
// }
// });
// $(function(){
// $(".nav li").mouseenter(function(){
// $(this).children("span").stop().animate({top:0});
// var index=$(this).index();
// $("audio").get(index).load();
// $("audio").get(index).play();
// });
// $(".nav li").mouseleave(function(){
// $(this).children("span").stop().animate({top:60});
// });
//
// var flag=true;
// $(document).on("keydown",function(e){
// if(flag){
// flag=false;
// var code=e.keyCode;
// if(code>=49 && code<=57){
// $(".nav li").eq(code-49).trigger("mouseenter");
// }
// }
// });
// $(document).on("keyup",function(e){
// flag=true;
// var code=e.keyCode;
// if(code>=49 && code<=57){
// $(".nav li").eq(code-49).trigger("mouseleave");
// }
// });
// });
$(function(){
$(".nav li").mouseenter(function(){
$(this).children("span").stop().animate({top:0});
var index=$(this).index();
$("audio").get(index).load();
$("audio").get(index).play();
});
$(".nav li").mouseleave(function(){
$(this).children("span").stop().animate({top:60});
});
var flag=true;
$(document).on("keydown",function(e){
if(flag){
flag=false;
var code=e.keyCode;
if(code>=49 && code<=57){
$(".nav li").eq(code-49).trigger("mouseenter");
}
}
});
$(document).on("keyup",function(e){
flag=true;
var code=e.keyCode;
if(code>=49 && code<=57){
$(".nav li").eq(code-49).trigger("mouseleave");
}
});
});
</script>
</head>
<body>
<div class="nav">
<ul>
<li><a href="#">1</a><span></span></li>
<li><a href="#">2</a><span></span></li>
<li><a href="#">3</a><span></span></li>
<li><a href="#">4</a><span></span></li>
<li><a href="#">5</a><span></span></li>
<li><a href="#">6</a><span></span></li>
<li><a href="#">7</a><span></span></li>
<li><a href="#">8</a><span></span></li>
<li><a href="#">9</a><span></span></li>
</ul>
<div>
<audio src="mp3/1.ogg"></audio>
<audio src="mp3/2.ogg"></audio>
<audio src="mp3/3.ogg"></audio>
<audio src="mp3/4.ogg"></audio>
<audio src="mp3/5.ogg"></audio>
<audio src="mp3/6.ogg"></audio>
<audio src="mp3/7.ogg"></audio>
<audio src="mp3/8.ogg"></audio>
<audio src="mp3/9.ogg"></audio>
</div>
</div>
</body>
</html>
四、delegate(selector,[type],[data],fn)
指定的元素(属于被选元素的子元素)添加一个或多个事件处理程序,并规定当这些事件发生时运行的函数。
使用 delegate() 方法的事件处理程序适用于当前或未来的元素(比如由脚本创建的新元素)。
当点击鼠标时,隐藏或显示 p 元素:
HTML 代码:
<div style="background-color:red">
<p>这是一个段落。</p>
<button>请点击这里</button>
</div>jQuery 代码:
$("div").delegate("button","click",function(){
$("p").slideToggle();
});
五、toggle([speed],[easing],[fn])
用于绑定两个或多个事件处理器函数,以响应被选元素的轮流的 click 事件。
如果元素是可见的,切换为隐藏的;如果元素是隐藏的,切换为可见的。
$("td").toggle(
function () {
$(this).addClass("selected");
},
function () {
$(this).removeClass("selected");
}
);
六、scroll([[data],fn])
当用户滚动指定的元素时,会发生 scroll 事件。
scroll 事件适用于所有可滚动的元素和 window 对象(浏览器窗口)。
$("div").scroll(function() {
$("span").text(x+=1);
});