Vue.js 实战教程 V2.x(11)事件处理

 图片

11事件处理
11.1监听事件
可以用 v-on 指令监听 DOM 事件,并在触发时运行一些 JavaScript 代码。
 
示例:
 
<div id="example-1">
 
  <button v-on:click="counter += 1">Add 1</button>
 
  <p>The button above has been clicked {{ counter }} times.</p>
 
</div>
 
var example1 = new Vue({
 
  el: '#example-1',
 
  data: {
 
    counter: 0
 
  }
 
})
 
11.2事件处理方法
 v-on 还可以接收一个需要调用的方法名称。
 
示例:
 
<div id="example-2">
 
  <!-- `greet` 是在下面定义的方法名 -->
 
  <button v-on:click="greet">Greet</button>
 
</div>
 
var example2 = new Vue({
 
  el: '#example-2',
 
  data: {
 
    name: 'Vue.js'
 
  },
 
  // 在 `methods` 对象中定义方法
 
  methods: {
 
    greet: function (event) {
 
      // `this` 在方法里指向当前 Vue 实例
 
      alert('Hello ' + this.name + '!')
 
      // `event` 是原生 DOM 事件
 
      if (event) {
 
        alert(event.target.tagName)
 
      }
 
    }
 
  }
 
})
 
// 也可以用 JavaScript 直接调用方法
 
example2.greet() // => 'Hello Vue.js!'
 
11.3内联处理器中的方法
除了直接绑定到一个方法,也可以在内联 JavaScript 语句中调用方法:
 
<div id="example-3">
 
  <button v-on:click="say('hi')">Say hi</button>
 
  <button v-on:click="say('what')">Say what</button></div>
 
new Vue({
 
  el: '#example-3',
 
  methods: {
 
    say: function (message) {
 
      alert(message)
 
    }
 
  }
 
})
 
有时也需要在内联语句处理器中访问原始的 DOM 事件。可以用特殊变量 $event 把它传入方法:
 
<button v-on:click="warn('Form cannot be submitted yet.', $event)">
 
  Submit</button>
 
// ...
 
methods: {
 
  warn: function (message, event) {
 
    // 现在我们可以访问原生事件对象
 
    if (event) event.preventDefault()
 
    alert(message)
 
  }
 
}
 
11.4事件修饰符
Vue.js 为 v-on 提供了事件修饰符。之前提过,修饰符是由点开头的指令后缀来表示的。
 
.stop
 
.prevent
 
.capture
 
.self
 
.once
 
.passive
 
<!-- 阻止单击事件继续传播 --><a v-on:click.stop="doThis"></a>
 
<!-- 提交事件不再重载页面 --><form v-on:submit.prevent="onSubmit"></form>
 
<!-- 修饰符可以串联 --><a v-on:click.stop.prevent="doThat"></a>
 
<!-- 只有修饰符 --><form v-on:submit.prevent></form>
 
<!-- 添加事件监听器时使用事件捕获模式 --><!-- 即元素自身触发的事件先在此处理,然后才交由内部元素进行处理 --><div v-on:click.capture="doThis">...</div>
 
<!-- 只当在 event.target 是当前元素自身时触发处理函数 --><!-- 即事件不是从内部元素触发的 --><div v-on:click.self="doThat">...</div>
 
2.1.4 新增
 
<!-- 点击事件将只会触发一次 --><a v-on:click.once="doThis"></a>
 
2.3.0 新增
 
Vue 还对应 addEventListener 中的 passive 选项提供了 .passive 修饰符。
 
<!-- 滚动事件的默认行为 (即滚动行为) 将会立即触发 -->
 
<!-- 而不会等待 `onScroll` 完成  -->
 
<!-- 这其中包含 `event.preventDefault()` 的情况 -->
 
<div v-on:scroll.passive="onScroll">...</div>
 
11.5按键修饰符
Vue 允许为 v-on 在监听键盘事件时添加按键修饰符:
 
<!-- 只有在 `key` 是 `Enter` 时调用 `vm.submit()` --><input v-on:keyup.enter="submit">
 
你可以直接将 KeyboardEvent.key 暴露的任意有效按键名转换为 kebab-case 来作为修饰符。
 
<input v-on:keyup.page-down="onPageDown">
 
在上述示例中,处理函数只会在 $event.key 等于 PageDown 时被调用。
 
11.6系统修饰键
2.1.0 新增
 
可以用如下修饰符来实现仅在按下相应按键时才触发鼠标或键盘事件的监听器。
 
.ctrl
 
.alt
 
.shift
 
.meta
 
例如:
 
<!-- Alt + C --><input @keyup.alt.67="clear">
 
<!-- Ctrl + Click --><div @click.ctrl="doSomething">Do something</div>
 
.exact 修饰符
2.5.0 新增
 
.exact 修饰符允许你控制由精确的系统修饰符组合触发的事件。
 
<!-- 即使 Alt 或 Shift 被一同按下时也会触发 -->
 
<button @click.ctrl="onClick">A</button>
 
<!-- 有且只有 Ctrl 被按下的时候才触发 -->
 
<button @click.ctrl.exact="onCtrlClick">A</button>
 
<!-- 没有任何系统修饰符被按下的时候才触发 -->
 
<button @click.exact="onClick">A</button>
 
鼠标按钮修饰符
2.2.0 新增
 
.left
 
.right
 
.middle
 
这些修饰符会限制处理函数仅响应特定的鼠标按钮。
 
 
 
完整代码:
 
11 事件处理1.html
 
<!DOCTYPE html>
 
<html>
 
<head>
    
<title>事件处理</title>
    
 
</head>
 
<body>
<div id="example-1">
  <button v-on:click="counter += 1">Add 1</button>
  <p>The button above has been clicked {{ counter }} times.</p>
</div>
 
<script>
    
var example1 = new Vue({
  el: '#example-1',
  data: {
    counter: 0
  }
})
 
</script>
 
</body>
 
</html>
 
 
11 事件处理2.html
 
<!DOCTYPE html>
 
<html>
 
<head>
    
<title>事件处理</title>
    
 
</head>
 
<body>
<div id="example-2">
  <!-- `greet` 是在下面定义的方法名 -->
  <button v-on:click="greet">Greet</button>
</div>
 
<script>
    
var example2 = new Vue({
  el: '#example-2',
  data: {
    name: 'Vue.js'
  },
  // 在 `methods` 对象中定义方法
  methods: {
    greet: function (event) {
      // `this` 在方法里指向当前 Vue 实例
      alert('Hello ' + this.name + '!')
      // `event` 是原生 DOM 事件
      if (event) {
        alert(event.target.tagName)
      }
    }
  }
})
// 也可以用 JavaScript 直接调用方法
example2.greet() // => 'Hello Vue.js!'
 
</script>
 
</body>
 
</html>
 
 
11 事件处理3.html
 
<!DOCTYPE html>
 
<html>
 
<head>
    
<title>事件处理</title>
    
 
</head>
 
<body>
<div id="example-3">
  <button v-on:click="say('hi')">Say hi</button>
  <button v-on:click="say('what')">Say what</button>
</div>
 
<script>
    
new Vue({
  el: '#example-3',
  methods: {
    say: function (message) {
      alert(message)
    }
  }
})
 
</script>
 
</body>
 
</html>
 
 
11 事件处理4.html
 
<!DOCTYPE html>
 
<html>
 
<head>
    
<title>事件处理</title>
    
 
</head>
 
<body>
<div id="example-4">
  <button v-on:click="warn('Form cannot be submitted yet.', $event)">
    Submit
  </button>
</div>
 
<script>
    
new Vue({
  el: '#example-4',
  methods: {
    warn: function (message, event) {
      // 现在我们可以访问原生事件对象
      if (event) event.preventDefault()
      alert(message)
    }
  }
})
 
</script>
 
</body>
 
</html>
 
 
11 事件处理5.html
 
<!DOCTYPE html>
 
<html>
 
<head>
    
<title>事件处理</title>
    
 
</head>
 
<body>
<div id="example-5">
  <!-- 阻止单击事件继续传播 -->
  <div v-on:click="count">
<a v-on:click.stop="doThis">doThis</a>
  </div>
  
  <!-- 提交事件不再重载页面 -->
  <form v-on:submit.prevent="onSubmit">
<button>submit</button>
  </form>
  
  <!-- 修饰符可以串联 -->
  <div v-on:click="count">
    <a v-on:click.stop.prevent="doThat">doThat</a>
  </div>
  
  <!-- 只有修饰符 -->
  <form v-on:submit.prevent>
<button>submit</button>
  </form>
 
  <!-- 添加事件监听器时使用事件捕获模式 -->
  <!-- 即元素自身触发的事件先在此处理,然后才交由内部元素进行处理 -->
  <div v-on:click.capture="doThis1">
    <div v-on:click="doThis2">
doThis
    </div>
  </div>
  
  <!-- 只当在 event.target 是当前元素自身时触发处理函数 -->
  <!-- 即事件不是从内部元素触发的 -->
  <div v-on:click="doThat1">
    <div v-on:click.self="doThat2">
doThat
    </div>
  </div>
  
  <!-- 点击事件将只会触发一次 -->
  <a v-on:click.once="doThis">doThis</a>
 
</div>
 
<script>
    
new Vue({
  el: '#example-5',
  methods: {
count: function () {
      alert("count")
    },
    doThis: function () {
      alert("doThis")
    },
    onSubmit: function () {
      alert("onSubmit")
    },
    doThat: function () {
      alert("doThat")
    },
    doThis1: function () {
      alert("doThis1")
    },
    doThis2: function () {
      alert("doThis2")
    },
    doThat1: function () {
      alert("doThat1")
    },
    doThat2: function () {
      alert("doThat2")
    }
  }
})
 
</script>
 
</body>
 
</html>
 
 
11 事件处理6.html
 
<!DOCTYPE html>
 
<html>
 
<head>
    
<title>事件处理</title>
    
 
</head>
 
<body>
<div id="example-6">
  <!-- 只有在 `key` 是 `Enter` 时调用 `vm.submit()` -->
  <input v-on:keyup.enter="submit">
  
  <input v-on:keyup.page-down="onPageDown">
 
</div>
 
<script>
    
new Vue({
  el: '#example-6',
  methods: {
submit: function () {
      alert("submit")
    },
onPageDown: function () {
      alert("onPageDown")
    }
  }
})
 
</script>
 
</body>
 
</html>
 
 
11 事件处理7.html
 
<!DOCTYPE html>
 
<html>
 
<head>
    
<title>事件处理</title>
    
 
</head>
 
<body>
<div id="example-7">
  <!-- Alt + C -->
  <input @keyup.alt.67="clear">
  
  <!-- Ctrl + Click -->
  <div @click.ctrl="doSomething">Do something
  </div>
  
  <!-- 即使 Alt 或 Shift 被一同按下时也会触发 -->
  <button @click.ctrl="onClick">A</button>
  
  <!-- 有且只有 Ctrl 被按下的时候才触发 -->
  <button @click.ctrl.exact="onCtrlClick">A</button>
 
  <!-- 没有任何系统修饰符被按下的时候才触发 -->
  <button @click.exact="onClick">A</button>
  
  <button @click.left="left">left</button>
  <button @click.right="right">right</button>
  <button @click.middle="middle">middle</button>
 
</div>
 
<script>
    
new Vue({
  el: '#example-7',
  c,
doSomething: function () {
      alert("doSomething")
    },
onClick: function () {
      alert("onClick")
    },
onCtrlClick: function () {
      alert("onCtrlClick")
    },
left: function () {
      alert("left")
    },
right: function () {
      alert("right")
    },
middle: function () {
      alert("middle")
    }
  }
})
 
</script>
 
</body>
 
</html>
 
欢迎观看视频教程:https://ke.qq.com/course/432961?tuin=36914f34,如有疑问,请加QQ群665714453交流讨论。
 

猜你喜欢

转载自www.cnblogs.com/daqiang123/p/11368417.html
今日推荐