美文网首页
绑定事件bind

绑定事件bind

作者: 暴走的金坤酸奶味 | 来源:发表于2018-09-25 07:57 被阅读0次

click


$(function(){
            只能绑定click事件,不能绑定其他的了
             $('#btn').click(function() {
            //  /* Act on the event */
            });

bind方式可绑定多个事件
            $('#btn').bind('click mouseover', function() {
                alert('hello!');

                //取消绑定事件
                $(this).unbind('mouseover');

主动触发与自定义事件

主动触发

可使用jquery对象上的trigger方法来触发对象上绑定的事件。

自定义事件

除了系统事件外,可以通过bind方法自定义事件,然后用tiggle方法触发这些事件。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>自定义事件</title>
    <style type="text/css">
        
    </style>
    <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
    <script type="text/javascript">
        $(function(){
            //自定义事件只能用bind方式绑定,第一个参数是事件的名字,第二个参数是事件发生时执行的函数
            $('#btn1').bind('hello', function(){
                alert('hello');
            })
            $('#btn1').bind('click', function(){
                alert('click');
            })
            $('#btn2').click(function() {
                // trigger即可以触发自定义事件,也可以触发原始的事件
                $('#btn1').trigger('hello');
                $('#btn1').trigger('click');
            });
            
            //不一定点击按钮触发,也可页面加载时触发,也可在满足某种if条件时触发
            // $('#btn1').trigger('hello');
        })
    </script>
</head>
<body>
    <input type="button" value="按钮" id="btn1">
    <input type="button" value="按钮2" id="btn2">
</body>
</html>

相关文章

  • jQuery(3)

    一 事件绑定 绑定 $().bind(事件类型,function(){}); $().bind(事件类型1,事件类...

  • Jquery事件处理

    绑定事件 bind(map)方法 bind(type, [data], fn)方法 在为对象绑定事件的同时,为事件...

  • 绑定和移除事件的方法

    1.bind()绑定事件 2.unbind()移除事件 3.on()绑定事件---bind 4.off()移除事件...

  • 2019-04-11 代理模式

    bind(,,,function(){} ) 绑定多个事件 bind 自定义绑定事件 事件冒泡 点击事件会一级一级...

  • jQuery中绑定事件时bind和on的区别

    jquery的bind跟on绑定事件的区别:主要是事件冒泡;jquery文档中bind和on函数绑定事件的用法: ...

  • jquery的bind跟on绑定事件的区别

    jquery的bind跟on绑定事件的区别:主要是事件冒泡; jquery文档中bind和on函数绑定事件的用法:...

  • 前端知识点(16)

    绑定事件bind $(function(){ // //只能绑定click事件,不能绑定其他的了 // $('#b...

  • jQuery的基础事件篇

    基础事件 1.绑定事件 jQuery 通过.bind()方法来为元素绑定这些事件。可以传递三个参数:bind(ty...

  • input框事件

    input框事件 绑定事件bind 自定义事件

  • 绑定事件 自定义事件 事件冒泡

    绑定事件 bind命令同时绑定多个事件 unbind取消绑定 自定义事件 trigger是触发事件 事件冒泡 ev...

网友评论

      本文标题:绑定事件bind

      本文链接:https://www.haomeiwen.com/subject/ksrkoftx.html