jq on綁定事件off移除事件


https://www.cnblogs.com/sandraryan/

以前用的是bind(); 后來更新后用的on

(on() 方法是 bind()、live() 和 delegate() 方法的新的替代品)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <div class="box1">hhhhhhhh</div>
    <div class="box2">hhhhhhhh</div>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script> <script> // bind(); 舊的綁定事件的方法 $('.box1').on('click mouseover mouseout',function(ev){ // var e = ev||event; console.log(233); }); // 添加不同事件,執行相同事件處理程序 $('.box2').on({ click: function(){ console.log('click'); }, mouseover : function(){ console.log('mouseover'); }, mouseout : function(){ console.log('mouseout'); } });
     // 添加不同時間的不同執行程序
</script>

</body>
</html>

 off 移除事件

on

給元素下子元素綁定事件

   <div class="wrap">
        <h2>this is h2</h2>
        <p>this is a para</p>
        <mark>mark text<mark>
        <div>div11111111</div>
        <div>div22222222</div>
    </div>


 $('.wrap').on('click','div',function(){
            $(this).css({
                fontSize: '40px',
                color :'red'
                });
        });
        // 把一個js原生對象作為實參傳到$函數中,就能拿到這個原生對象對應的jq對象,
        // 然后調用jq操作函數

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM