「Vue.js」v-on:mouseoutでmouseoutイベントを取得する
環境
Windows10 home 64bit
Vue.js 3.2.2
Google Chrome 99.0.4844.51
書式
<div @mouseout=イベントA @mouseover=イベントB >
methods: {
イベントA: function () {
処理コード
},
イベントB: function () {
処理コード
}
}
<div @mouseout=イベントA @mouseover=イベントB >
methods: {
イベントA: function () {
処理コード
},
イベントB: function () {
処理コード
}
}
<div @mouseout=イベントA @mouseover=イベントB > methods: { イベントA: function () { 処理コード }, イベントB: function () { 処理コード } }
「v-on:mouseout」を使用して、mouseoutイベントを取得します。
使用例
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>mousemoveイベントサンプル</title>
<script src="https://unpkg.com/vue@next"></script>
</head>
<body>
<div id="app" >
<h2 >{{val}}</h2>
<div @mouseout="cftover" @mouseover="cftout" >
テスト文字
</div>
</div>
<script>
const kdf = {
data() {
return {
val: '',
msgA: 'mouseoutされました',
msgB: 'mouseoverされました',
}
},
methods: {
cftover: function () {
//テキスト表示
this.val = this.msgA
//alert表示
alert(this.msgA)
},
cftout: function () {
//テキスト表示
this.val = this.msgB
//alert表示
alert(this.msgB)
}
}
}
Vue.createApp(kdf).mount('#app')
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>mousemoveイベントサンプル</title>
<script src="https://unpkg.com/vue@next"></script>
</head>
<body>
<div id="app" >
<h2 >{{val}}</h2>
<div @mouseout="cftover" @mouseover="cftout" >
テスト文字
</div>
</div>
<script>
const kdf = {
data() {
return {
val: '',
msgA: 'mouseoutされました',
msgB: 'mouseoverされました',
}
},
methods: {
cftover: function () {
//テキスト表示
this.val = this.msgA
//alert表示
alert(this.msgA)
},
cftout: function () {
//テキスト表示
this.val = this.msgB
//alert表示
alert(this.msgB)
}
}
}
Vue.createApp(kdf).mount('#app')
</script>
</body>
</html>
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="utf-8"> <title>mousemoveイベントサンプル</title> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="app" > <h2 >{{val}}</h2> <div @mouseout="cftover" @mouseover="cftout" > テスト文字 </div> </div> <script> const kdf = { data() { return { val: '', msgA: 'mouseoutされました', msgB: 'mouseoverされました', } }, methods: { cftover: function () { //テキスト表示 this.val = this.msgA //alert表示 alert(this.msgA) }, cftout: function () { //テキスト表示 this.val = this.msgB //alert表示 alert(this.msgB) } } } Vue.createApp(kdf).mount('#app') </script> </body> </html>
実行結果
マウスがdiv要素「テキスト文字」に入ると、「mouseoutされました」というテキストとalertメッセージが表示されます。
マウスがdiv要素「テキスト文字」から外れると、「mouseoverされました」というテキストとalertメッセージが表示されます。