博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
jQuery事件
阅读量:6714 次
发布时间:2019-06-25

本文共 5123 字,大约阅读时间需要 17 分钟。

1.事件函数列表

(1)click鼠标事件

(2)mouseover() 鼠标进入(进入子元素也触发)

(3)mouseout() 鼠标离开(离开子元素也触发)
(4)mouseenter()鼠标进入(进入子元素不触发)
(5)mouseleave()鼠标离开(离开子元素不触发)
(6)hover()

$(function(){

/*移入,子元素也会触发*/    /*$('.box1').mouseover(function(){        alert('移入');    })*/    /*移出,子元素也会触发*/    /*$('.box1').mouseout(function(){        alert('移出');    })*/    /*移入移出,子元素不会触发,hover是合并写法*/    $('.box1').mouseenter(function(){        alert('移入');    })    $('.box1').mouseleave(function(){        alert('移出');    })})

(7)ready()DOM加载完成

(8)resize()浏览器窗口的大小发生改变

$(window).resize(function(){

var $wr = $(window).width();
document.title = $wr;
})

(9)scroll()滚动条的位置发生变化

(10)submit()用户递交表单

$(function(){

/一开始就获得焦点,元素只能一个获得焦点,blur失去焦点/
$('#ipt1').focus();
/$('#ipt2').focus();/
$('#fm1').submit(function(){
/alert('提交');/
/拒绝提交/
return false;
})
})

(11)blur()元素失去焦点

(12)focus()元素获得焦点

$(function(){

/一开始就获得焦点,元素只能一个获得焦点,blur失去焦点/
$('#ipt1').focus();
/$('#ipt2').focus();/
})

2.绑定事件的其他方式

<!doctype html>

<html>
<head>
<meta charset="utf-8">
<title>绑定事件</title>
<script type="text/javascript" src="../jQuery库/jquery-3.3.1.min.js"></script>
<script type="text/javascript">

$(function(){    /*click事件    $('#btn').click(function(){        alert('click')    })    */    /*移入和点击都触发*/    $('#btn').bind('mouseover click',function(){        alert('bind');        /*第二次进入解绑*/        $(this).unbind('mouseover');    })})

</head>

<body>

</body>

</html>

3.事件冒泡

在一个对象上触发某类事件(比如单击onclick),如果此对象定义了此事件的处理程序,那么此事件就会调用这个处理程序,如果没有定义此事件处理程序或者事件返回true,那么这个事件会向这个对象的父级对象传播,从里到外,直至它被处理(父级对象所有同类事件都将被激活),或者它达到了对象层次的最顶层,即document对象(有些浏览器是window)

4.事件冒泡的作用

事件冒泡允许多个操作被集中处理(把事件处理添加到一个父级元素上,避免把事件处理器添加到多个子级元素上),它还可以让你在对象层的不同级别捕获事件。

5.阻止事件冒泡

事件冒泡机制有时候是不需要的,需要阻止掉,通过event.stopPropagation()来阻止

<!doctype html>

<html>
<head>
<meta charset="utf-8">
<title>事件冒泡</title>
<script type="text/javascript" src="../jQuery库/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
$(function(){

/*事件往上传,弹出123,a表示事件对象,一般写event*/    $('.son').click(function(a){        alert(1);        /*阻止事件冒泡*/        a.stopPropagation();    })    /*弹出23*/    $('.father').click(function(){        alert(2);    })    /*弹出3*/    $('.grandfather').click(function(){        alert(3);        /*第二种阻止事件写法*/        return false;    })    /*整个文档最顶级*/    $(document).click(function(){        alert(4);    })})

<style type="text/css">

.grandfather{    width: 300px;    height: 300px;    background-color: antiquewhite;    cursor: pointer;}.father{    width: 200px;    height: 200px;    background-color: indianred;    cursor: pointer;}.son{    width: 100px;    height: 100px;    background-color: tan;    cursor: pointer;}

</head>

<body>

</body>

</html>

6.阻止默认行为

阻止表单提交

7.合并阻止

一般把阻止冒泡和阻止默认行为合并起来写,合并写法可以用

$('.grandfather').click(function(){

alert(3);
/第二种阻止事件写法,合并写法/
return false;
})

例子:弹框

<!doctype html>

<html>
<head>
<meta charset="utf-8">
<title>弹框</title>
<script type="text/javascript" src="../jQuery库/jquery-3.3.1.min.js"></script>
<script type="text/javascript">

$(function(){    $('.btn').click(function(){        $('.pop_con').fadeIn();        return false;    })    $(document).click(function(){        $('.pop_con').fadeOut();    })    $('.pop1').click(function(){        return false;    })    $('#off').click(function(){        $('.pop_con').fadeOut();    })})

<style type="text/css">

.pop_con{    display: none;}.pop1{    width: 300px;    height: 300px;    border: 3px solid #000;    background-color: #87CEF5;    position: fixed;    left: 50%;    top: 50%;    margin-left: -150px;    margin-top: -150px;    z-index: 100;}.pop2{    width: 100%;    height: 100%;    background-color: #000000;    opacity: 0.3;    filter: alpha(opacity=30);    position: fixed;    left: 0;    top: 0;    z-index: 98;}.close{    font-size: 30px;    text-decoration: none;    color: red;    float: right;}

</head>

<body>

弹框文字 输入颜值:
x

</body>

</html>

jQuery事件

8.事件委托

事件委托就是利用冒泡的原理,把事件加到父级上,通过判断事件来源的子集,执行相应的操作,事件委托首先可以极大减少事件绑定次数,提高性能,其次可以让新加入的子元素也可以拥有相同的操作

<!doctype html>

<html>
<head>
<meta charset="utf-8">
<title>事件委托</title>
<script type="text/javascript" src="../jQuery库/jquery-3.3.1.min.js"></script>
<script type="text/javascript">

$(function(){    //普通写法,性能不高,新加入的li需要重新绑定    $('.list li').click(function(){        $(this).css({'backgroundColor':'red'});    })    /*    var $li = $('
  • 9
  • ') $('.list').append($li); */ /*重新绑定,$li.click(....)*/ /*事件委托,父级list受委托,li的委托,click事件,函数*/ $('.list').delegate('li','click',function(){ $(this).css({'backgroundColor':'red'}); }) /*事件委托不用重新绑定*/ var $li = $('
  • 9
  • ') $('.list').append($li);})

    <style type="text/css">

    .list{    width: 500px;    height: 400px;    background-color: antiquewhite;    text-align: center;    list-style: none;    padding: 0;}.list li{    width: 500px;    height: 40px;    background-color: aquamarine;    margin: 10px auto;}

    </head>

    <body>

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    </body>

    </html>

    jQuery事件

    转载于:https://blog.51cto.com/13742773/2341811

    你可能感兴趣的文章
    我的友情链接
    查看>>
    RocketMQ之去重操作
    查看>>
    linux:Bash常用快捷键
    查看>>
    《学习GNU Emacs》学习笔记之三 —— 查找和替换操作
    查看>>
    64位linux yum安装32位的rpm包
    查看>>
    centos 6 运行docker 后ssh 连不进容器问题
    查看>>
    Windows Server 2008 R2: Failover Clustering Troubleshooting 场景
    查看>>
    ganglia运维监控解决方案及与nagios监控整合
    查看>>
    我的友情链接
    查看>>
    mysql之union
    查看>>
    第一个提出“云计算”概念的人
    查看>>
    发现java的swing的jframe应该使用jpanel来布局
    查看>>
    安卓的生命周期和布局大概
    查看>>
    [转]xshell的快捷键(非常实用)
    查看>>
    虚拟机C盘空间不足解决方法
    查看>>
    对 Web 应用程序进行性能调优
    查看>>
    Mac OS 配置多个ssh-key
    查看>>
    桌面支持--excel每页打印表头
    查看>>
    Openssl CA认证
    查看>>
    VSCode 配置python开发环境
    查看>>