美文网首页
jQuery事件绑定

jQuery事件绑定

作者: Dxes | 来源:发表于2019-12-12 17:41 被阅读0次

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="js/jquery.min.js">

    </script>
</head>
<body>
    <div id="div1">
        <button>按钮1</button>
        <button>按钮2</button>
        <button>按钮3</button>
    </div>
    <button id="addBtn">添加</button>
    
    <hr />
    <div id="div2" style="background-color: yellow;">
        <!--<p>我是段落1</p>-->
        <button>按钮4</button>
        <button>按钮5</button>
        <!--<p>我是段落2</p>-->
        <button>按钮6</button>
    </div>
    <button id="addBtn2">添加</button>
    <!---------1.事件绑定-------------->
    <script type="text/javascript">
        //1)节点对象.on(事件名,事件驱动程序)  -  直接给指定的节点绑定事件
        //注意: 事件名要去掉on
        $('#div1>button').on('click', function(evt){
            //注意: 这儿的this是事件源,但是是js的对象
            console.log(this)
            
            $(this).css('color', 'red')
            
            //事件捕获:和js一样
            evt.stopPropagation()
        })
        
        console.log($('button').text())    // 按钮1按钮2按钮3
        
        
        $('#addBtn').on('click', function(){
            $('#div1').append($('<button>新按钮</button>'))
        })
        
        
        //2)节点对象.on(事件名,选择器,事件驱动程序)  -   通过父标签给选择器选中的子标签绑定事件
        $('#div2').on('click', 'button',function(){
            alert('按钮点击')
            console.log(this)
                
        })
        
        $('#addBtn2').on('click', function(){
            $('#div2').append($('<button>新按钮</button>'))
        })
        
        
        
    </script>
</body>

</html>

相关文章

  • Jquery day_3

    1.1 Jquery 事件注册 1.2 jquery 事件处理 on(): 用于事件绑定,目前最好用的事件绑定方...

  • jquery对节点的操作

    Jquery对事件的绑定 $().bind(“事件类型”, 事件处理); 给jquery绑定一个事件$().bi...

  • 通过jquery动态添加的元素绑定事件

    1.一般通过jquery绑定事件# 2.通过jquery添加的元素绑定事件#

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

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

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

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

  • 03-jQuery事件相关

    事件绑定与解绑 jQuery中有两种绑定事件方式eventName(fn);编码效率略高/部分事件jQuery没有...

  • jQuery事件

    1、jquery 如何绑定事件?直接绑定和使用事件代理分别如何使用 jquery在1.7版本之前绑定事件可通过bi...

  • jQuery知识整理

    jQuery jQuery和DOM关系 jquery框架对象分析 加载事件 事件绑定 动画效果 jquery封装的...

  • jquery事件

    jQuery其他事件 绑定事件 自定义事件

  • 从零玩转jQuery-事件处理

    事件绑定 jQuery中事件绑定有两种方式eventName(function(){})绑定对应事件名的监听, ...

网友评论

      本文标题:jQuery事件绑定

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