1. 程式人生 > 其它 >jQuery筆記-事件1

jQuery筆記-事件1

一ready

ready與js中window.load一樣,讓函式在頁面其他元素都載入記載完畢後在執行,有了這個函式,函式就可以寫在html任意位置,不用擔心因為不小心在頁面元素前寫了元素的函式,導致不執行或報錯

on

在選擇元素上繫結一個或多個事件的事件處理函式。

例子:放在click me上面輸出move,點選輸出down

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script 
src="node_modules/jquery/dist/jquery.min.js"></script> </head> <body> <div id="div1">click me</div> <ul> <li class="liC">1</li> <li class="liC">2</li> <li>3</li> <li class="liC">4</li> <li>5</li>
<li>6</li> <li class="liC">7</li> <li class="liC">8</li> </ul> <ul></ul> <input type="text"> <input type="password"> </body> </html> <script> const div1=document.getElementById('div1')//js獲取dom // $('#div1')//jquery獲取dom
// js dom和jq dom可以相互轉換 console.log($(div1))//js dom ==>jq dom console.log($('#div1')[0])//jq dom==>js dom console.log($('.liC')) console.log($('ul')) console.log($('ul>li')) console.log($('ul,div')) console.log($('#div1+ul')) console.log($('#div1~ul')) $('#div1').on('mousemove',function () { console.log('move') }) $('#div1').on('mousedown',function () { console.log('down') }) </script>

off()

off() 方法移除用.on()繫結的事件處理程式。

例子1:清除上個案例on的事件

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="node_modules/jquery/dist/jquery.min.js"></script>
</head>
<body>
<div id="div1">click me</div>
<ul>
    <li class="liC">1</li>
    <li class="liC">2</li>
    <li>3</li>
    <li class="liC">4</li>
    <li>5</li>
    <li>6</li>
    <li class="liC">7</li>
    <li class="liC">8</li>
</ul>
<ul></ul>
<input type="text">
<input type="password">
</body>
</html>
<script>
    const div1=document.getElementById('div1')//js獲取dom
    // $('#div1')//jquery獲取dom

    // js dom和jq dom可以相互轉換
    console.log($(div1))//js dom ==>jq dom
    console.log($('#div1')[0])//jq dom==>js dom

    console.log($('.liC'))
    console.log($('ul'))
    console.log($('ul>li'))
    console.log($('ul,div'))
    console.log($('#div1+ul'))
    console.log($('#div1~ul'))
    $('#div1').on('mousemove',function () {
        console.log('move')
    })
    $('#div1').on('mousedown',function () {
        console.log('down')
    })
    $('#div1').off()


</script>

  例子2:清除上個案例on的事件中的mousemove事件

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="node_modules/jquery/dist/jquery.min.js"></script>
</head>
<body>
<div id="div1">click me</div>
<ul>
    <li class="liC">1</li>
    <li class="liC">2</li>
    <li>3</li>
    <li class="liC">4</li>
    <li>5</li>
    <li>6</li>
    <li class="liC">7</li>
    <li class="liC">8</li>
</ul>
<ul></ul>
<input type="text">
<input type="password">
</body>
</html>
<script>
    const div1=document.getElementById('div1')//js獲取dom
    // $('#div1')//jquery獲取dom

    // js dom和jq dom可以相互轉換
    console.log($(div1))//js dom ==>jq dom
    console.log($('#div1')[0])//jq dom==>js dom

    console.log($('.liC'))
    console.log($('ul'))
    console.log($('ul>li'))
    console.log($('ul,div'))
    console.log($('#div1+ul'))
    console.log($('#div1~ul'))
    $('#div1').on('mousemove',function () {
        console.log('move')
    })
    $('#div1').on('mousedown',function () {
        console.log('down')
    })
    $('#div1').off('mousemove')


</script>