jQuery-day04

A.我今天學(xué)了什么

1.jQ事件

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title></title>
        <style type="text/css">
            #btn{
                width: 120px;
                height: 35px;
                font-size: 18px;
            }
            .box{
                width: 500px;
                height: 500px;
                background: burlywood;
            }
            .box2{
                width: 200px;
                height: 200px;
                background: cornflowerblue;
            }
            #txt{
                width: 240px;
                height: 30px;
                margin-top: 30px;
            }
        </style>
        <script type="text/javascript" src="js/jquery-3.2.1.min.js" ></script>
        <script type="text/javascript">
            $(function(){
                var btn = $('#btn');
                var box = $('.box');
                var txt = $('#txt');
                
                //JQ事件
                //1.單擊事件 click()
                //原生JS中同一個(gè)對(duì)象綁定相同的事件的時(shí)候,后面的事件會(huì)覆蓋前面的事件;
                /*btn[0].onclick=function(){
                    alert('s')
                }
                btn[0].onclick=function(){
                    alert('d')
                }*/
                
                //jq中,同一個(gè)對(duì)象綁定相同的事件,不會(huì)被覆蓋,而是多個(gè)事件融合在一起
                /*btn.click(function(){
                    alert('a')
                })
                
                btn.click(function(){
                    alert('s')
                })*/
                
                //事件綁定數(shù)據(jù),不是局部變量
                /*btn.click(foo = 'avc',function(){
                    alert(foo)
                })
                box.click(function(){
                    alert(foo)
                })*/
                
                //hover()事件:相當(dāng)于同時(shí)整合了enter+leave事件
                /*box.mouseenter(function(){
                    console.log('我進(jìn)來了')
                })
                box.mouseleave(function(){
                    console.log('我出來了')
                })*/
                /*box.hover(function(){
                    console.log('我進(jìn)來了')
                },function(){
                    console.log('我出來啦')
                })*/
                /*$(document).keydown(function(e){
                    console.log(e.keyCode);
                })*/
                
                //窗口大小改變事件:
                /*$(window).resize(function(){
                    console.log('s')
                })*/
                //頁面滾動(dòng)事件
                /*$(window).scroll(function(){
                    console.log('s')
                })*/
                //文本選中事件
                /*txt.select(function(){
                    console.log('小米')
                })*/
                
                
            })
        </script>
    </head>
    <body>
        <button id="btn">button</button>
        <div class="box">
            <div class="box2">
                
            </div>
        </div>
        
        <input id="txt" type="text" />
    </body>
</html>

2.jQ事件的綁定

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style type="text/css">
            button{
                width: 150px;
                height: 40px;
                font-size: 20px;
            }
            ul{
                width: 500px;
                border: 1px solid #AAAAAA;
                padding: 10px;
                list-style: none;
            }
            .ds{
                width: 500px;
                height: 300px;
                background-color: #DEB887;
            }
        </style>
    </head>
    <script type="text/javascript" src="js/jquery-3.2.1.min.js" ></script>
    <script type="text/javascript">
        $(function(){
            //事件的綁定:
            //簡(jiǎn)單寫法:
            /*$('#btn').click(function(){
                alert('s')
            })*/
            
            //通過on方法,把他們綁定在了一起,就相當(dāng)于把click和function綁定在了一起
            //通過on方法聲明綁定的數(shù)據(jù)和直接click的區(qū)別
            //直接click相當(dāng)于聲明了變量
            //on則是把數(shù)據(jù)綁定在了click事件里面
            /*$('#btn').on('click',{foo:'a'},function(e){
                console.log(e.data.foo)
            })*/
            
            //平時(shí)開發(fā)者,倡導(dǎo)多用事件的綁定,少用click,事件的綁定可以加快運(yùn)行速度,減少資源損耗
            
            //指向元素的事件綁定(用法)
            /*$(document).on('click','li',function(){
                console.log('s')
            });*/
            
            //事件的解綁
            /*$('#btn').click(function(){
                alert('s')
            })
            $('#btn').click(null)*/
            
            /*$('#btn').on('click',function(){
                alert('s')
            })
            
            $('#btn').off('click')*/
            
            //事件綁定的命名空間
            //a.on('click.自定義名稱',function(){});
            /*$('#btn').on('click.sdfgasdgfsdgsdfgfg',function(){
                alert('s')
            });
            $('#btn').on('click.qwertwertdsgvsdr',function(){
                alert('d')
            });
            
            $('#btn').off('click.sdfgasdgfsdgsdfgfg')*/
            
            //on事件是在JQ1.7以后的坂本才出現(xiàn)的新東西
            //在1.7以前,咱們是用bind來進(jìn)行事件的綁定
            //bind和on效果完全一致,只不過因?yàn)樗惴ǖ膯栴},on更加效率,所以徹底替代了bind
            
            //on一次可以綁定多個(gè)事件,多個(gè)事件要在同一個(gè)引號(hào)里面,并且用空格隔開
            /*$('#btn').on('click mouseenter',function(){
                alert('s')
            });*/
            
            //事件的委托
            //在jq里面事件的委托,就是將事件綁定在上一級(jí)元   素上面,通過上一級(jí)元素來監(jiān)聽事件的觸發(fā);
            //事件的綁定
            /*$('.box li').on('click',function(){
                $('.box2').append($(this).clone())
            })*/
            
            /*$('.box').on('click','li',function(){
                console.log($(this).html())
            })*/
            
            //一次性事件 
            /*$('button').one('click',function(){
                alert('s')
            });*/
            
            
            
        })
    </script>
    <body>
        <button id="btn">button</button>
        <div>
            <ul class="box">
                <li>1</li>
                <li>2</li>
                <li>3</li>
                <li>4</li>
                <li>5</li>
                <li>6</li>
                <li>7</li>
                <li>8</li>
                <li>9</li>
                <li>10</li>
            </ul>
        </div>
        <div>
            <ul class="box2"></ul>
        </div>
        <div class="ds"></div>
        <input id="txt" type="text" style="width: 300px;height: 30px;margin-top: 50px;" />
        <a >跳轉(zhuǎn)到百度</a>
    </body>
</html>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,693評(píng)論 19 139
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,323評(píng)論 25 708
  • 調(diào)試分成兩個(gè)階段: 遠(yuǎn)程可以測(cè)試連接mysql的端口 如果無法鏈接,按下面過程分析 防火墻 查看相關(guān)mysql端口...
    金色的清晨閱讀 645評(píng)論 0 0
  • 今天醒來,朱先生夜班回來,從單位帶回了早餐。遂起床,打開廣播聽新聞,播放河北邢臺(tái)洪水受災(zāi)的事件,這時(shí)有個(gè)千古難題...
    盼angel閱讀 177評(píng)論 0 0
  • ?
    懷瑾小天使閱讀 156評(píng)論 0 0

友情鏈接更多精彩內(nèi)容