jQuery實例方法之on、one、off、trigger、hover、keydown

on:綁定事件

<!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>溫故而知"心"</title>
  <style>
    .demo {
      width: 100px;
      height: 100px;
      background-color: #ff0000;
    }

    .demo1 {
      width: 100px;
      height: 100px;
      background-color: #ff00ff;
    }
  </style>
</head>

<body>
  <div class="demo"></div>
  <div class="demo1"></div>
  <ul>
    <li>1</li>
    <li>2</li>
    <li>3</li>
  </ul>
  <ul>
    <li>4</li>
    <li>5</li>
    <li>6</li>
  </ul>
</body>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
  /*
    on
    系統事件
  */
  $('.demo').on('click', function () {
    alert("xyz")
  })

  $('.demo1').on('mouseenter', function () {
    alert("lm")
  })

  //點擊ul彈窗:xyzul
  $('ul').eq(0).on('click', function () {
    alert('xyzul')
  })
  //現在你想點擊ul裏面的li,彈窗出:xyzli
  $('ul').eq(1).on('click', 'li', () => {
    alert('xyzli')
  })
</script>

</html>

on通過對象的方式控制多個函數

<!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>溫故而知"心"</title>
</head>

<body>
  <div>xyz</div>
</body>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
  $('div').on({
    click: function () {
      console.log('click')
    },
    mouseenter: function () {
      console.log('mouseeenter')
    },
    mouseleave: function () {
      console.log('mouseleave')
    }
  })
</script>

</html>

one

<!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>溫故而知"心"</title>
</head>

<body>
  <a href="https://www.baidu.com">只跳轉一次淘寶頁面,後面一直跳轉百度頁面</a>
</body>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
  /*
    one:只使用一次
  */
  $('a').one('click', function () {
    window.open('https://taobao.com');
    return false;
  })
</script>

</html>

off:解綁事件

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>溫故而知"心"</title>
    <style>
        .demo {
            width: 100px;
            height: 100px;
            background-color: #ff0000;
        }
    </style>
</head>

<body>
    <div class="demo"></div>
</body>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
    function clickOne() {
        console.log("clickOne")
    }
    function clickTwo() {
        console.log("clickTwo")
    }
    $('.demo').on('click', clickOne)
    $('.demo').on('click', clickTwo)

    function enterOne() {
        console.log("enterOne")
    }
    function enterTwo() {
        console.log("enterTwo")
    }
    $('.demo').on('mouseenter', enterOne)
    $('.demo').on('mouseenter', enterTwo)

    //off: 解綁
    //解綁div類名爲demo的全部事件
    //    $('.demo').off()

    //解綁div類名爲demo的全部click事件
    //    $('.demo').off('click')

    //解綁div類名爲demo的click事件——clickOne
    //第二個參數是指定解綁事件的函數
    $('.demo').off('click', clickOne)
</script>

</html>

trigger

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>溫故而知"心"</title>
    <style>
        .demo {
            width: 100px;
            height: 100px;
            background-color: #ff0000;
        }
    </style>
</head>

<body>
    <div class="demo"></div>
</body>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
    //系統事件
    $('.demo').on('click', function (e, a, b, c, d) {
        console.log('click', a, b, c, d)
    })
    $('.demo').trigger('click', [11, 22, 4, 7])

    //自定義事件
    $('.demo').on('pageLoad', function (e, a, b, c, d) {
        console.log('click', a, b, c, d)
    })
    $('.demo').trigger('pageLoad', [77, 21, 123, 456])

</script>

</html>

hover方法:可以觸發鼠標移入移出的效果

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>溫故而知"心"</title>
    <style>
        .demo {
            width: 100px;
            height: 100px;
            background-color: #ff0000;
        }

        .demo1 {
            width: 100px;
            height: 100px;
            background-color: #0000ff;
        }
    </style>
</head>

<body>
    <div class="demo"></div>
    <div class="demo1"></div>
</body>

<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
    //hover:可以觸發鼠標移入移出的效果
    $('.demo').on('mouseenter', () => {
        console.log('enter')
    }).on('mouseleave', () => {
        console.log('leave')
    })

    $('.demo1').hover(() => {
        console.log('enter1')
    }, () => {
        console.log('leave1')
    })

</script>

</html>

keydown:鍵盤按下事件

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>溫故而知"心"</title>
</head>

<body>
    <input type="text" class="demo" />
</body>

<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
    //keydown:鍵盤按下事件
    $('.demo').keydown(function () {
        console.log($(this).val())
    })
</script>

</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章