最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

jQuery的事件處理你知道多少

 更新時間:2022年02月23日 17:10:53   作者:Han_Zhou_Z  
這篇文章主要為大家詳細(xì)介紹了jQuery的事件處理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助

一、jQuery的事件處理

1、頁面載入事件

$(document).ready() --- onload

2、事件綁定(bind)

bind(type,[data],fn)

type:表示事件類型(clickmouseover、mouseout...)

[data]:可選參數(shù),表示傳遞給事件對象的額外數(shù)據(jù)

fn:是一個函數(shù)(事件處理函數(shù)),當(dāng)事件發(fā)生時執(zhí)行的程序

為每一個匹配元素的特定事件(像click)綁定一個事件處理器函數(shù)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../jq/jquery.js"></script>
</head>
<body>
    <button id="btn">確定</button>
    <script>
        $(function(){
            $('#btn').bind('click',function(){//可以給按鈕綁定其他事件
                alert('事件綁定')
            })
        })
    </script>
</body>
</html>

 顯示效果:點(diǎn)擊確定按鈕之后,出現(xiàn)彈窗

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../jq/jquery.js"></script>
</head>
<body>
    <img src="../img/1.jpg" alt="" width="150" height="200">
    <script>
        $(function(){
            //通過鼠標(biāo)的懸停、離開事件來改變img的圖像
            $('img').bind('mouseover',function(){
                $(this).attr({src:'../img/2.jpg'})//this表示的是img這個元素
            })
            $('img').bind('mouseout',function(){
                $(this).attr({src:'../img/1.jpg'})
            })
        })
    </script>
</body>
</html>

 顯示效果:當(dāng)鼠標(biāo)懸停在圖片上時,顯示的是一個圖片。當(dāng)鼠標(biāo)離開這個圖片時,顯示的是另一張圖片。反復(fù)交替,沒有限制。

3、反綁定事件(unbind)

unbind([type],[data]):刪除綁定的事件

(1)不帶參數(shù):刪除元素上綁定的所有事件

(2)帶參數(shù):[type]表示事件類型

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../jq/jquery.js"></script>
</head>
<body>
    <img src="../img/1.jpg" alt="" width="150" height="200">
    <script>
        $(function(){
            //通過鼠標(biāo)的懸停、離開事件來改變img的圖像
            $('img').bind('mouseover',function(){
                $(this).attr({src:'../img/2.jpg'})//this表示的是img這個元素
            })
            $('img').bind('mouseout',function(){
                $(this).attr({src:'../img/1.jpg'})
            })
            $('img').unbind('mouseout')//解綁
        })
    </script>
</body>
</html>

 顯示效果:鼠標(biāo)離開圖片之后,圖片不會變成1.jpg

4、一次性事件綁定(one)

綁定的事件只能執(zhí)行一次

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../jq/jquery.js"></script>
</head>
<body>
    <img src="../img/1.jpg" alt="" width="150" height="200">
    <script>
        $(function(){
            //通過鼠標(biāo)的懸停、離開事件來改變img的圖像
            $('img').bind('mouseover',function(){
                $(this).attr({src:'../img/2.jpg'})//this表示的是img這個元素
            })
            //一次性事件綁定
            $('img').one('mouseout',function(){
                $(this).attr({src:'../img/1.jpg'})
            })
        })
    </script>
</body>
</html>

顯示效果:鼠標(biāo)離開圖片后,圖片會變成1.jpg,但是這種變化只會執(zhí)行一次。第二次離開圖片時,就不會變成1.jpg。

5、模擬鼠標(biāo)懸停(hover)

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <meta http-equiv="X-UA-Compatible" content="IE=edge">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>Document</title>    <script src="../jq/jquery.js"></script></head><body>    <div style="width: 200px; height: 200px; background-color: red;"></div>    <script>        $(function(){            $('div').hover(function(){                $(this).css('backgroundColor','pink')            })        })    </script></body></html><!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../jq/jquery.js"></script>
</head>
<body>
    <div style="width: 200px; height: 200px; background-color: red;"></div>
    <script>
        $(function(){
            $('div').hover(function(){
                $(this).css('backgroundColor','pink')
            })
        })
    </script>
</body>
</html>

顯示效果:鼠標(biāo)懸停在圖片上時,圖片由紅色變?yōu)榉凵kx開圖片時并不會變回原來的紅色。

總結(jié)

本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!   

相關(guān)文章

最新評論

满城县| 扶风县| 南充市| 桐庐县| 城市| 周至县| 天门市| 吐鲁番市| 东阳市| 赫章县| 阜新| 古交市| 南川市| 子洲县| 襄城县| 平顶山市| 华安县| 治县。| 象山县| 页游| 宜宾县| 吉林省| 荆州市| 福鼎市| 大石桥市| 友谊县| 秦皇岛市| 松滋市| 繁昌县| 涞水县| 商洛市| 嘉荫县| 隆回县| 安达市| 浠水县| 鹤峰县| 古浪县| 固安县| 河津市| 德钦县| 临洮县|