vue2.0的contextmenu右鍵彈出菜單的實(shí)例代碼
整理文檔,搜刮出一個(gè)vue2.0的contextmenu右鍵彈出菜單的實(shí)例代碼,稍微整理精簡一下做下分享。
1.事情對象
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8">
<script src="http://unpkg.com/vue/dist/vue.js"></script>
<script type="text/javascript">
window.onload = function(){
var vm = new Vue({
el:'#box',
methods:{
show:function(event){
console.log(event); //event 這個(gè)就是事件對象了
}
}
});
}
</script>
</head>
<body>
<div id="box">
<input type="button" value="按鈕" @click="show($event)">
</div>
</body>
</html>
通過show($event)把事件對象傳到方法里
2.事件冒泡
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8">
<script src="http://unpkg.com/vue/dist/vue.js"></script>
<script type="text/javascript">
window.onload = function(){
var vm = new Vue({
el:'#box',
methods:{
show:function(){
alert(1);
},
show1:function(){
alert(2);
}
}
});
}
</script>
</head>
<body>
<div id="box">
<div @click="show1()">
<input type="button" value="按鈕" @click="show()">
</div>
</div>
</body>
</html>
點(diǎn)擊按鈕的話他會,執(zhí)行show ,show1方法,依次彈出1,2。
怎么來阻止
<1> 利用我們上面講過的event對象: event.cancelBubble = true; //這種就阻止了
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8">
<script src="http://unpkg.com/vue/dist/vue.js"></script>
<script type="text/javascript">
window.onload = function(){
var vm = new Vue({
el:'#box',
methods:{
show:function(event){
alert(1);
event.cancelBubble = true;
},
show1:function(){
alert(2);
}
}
});
}
</script>
</head>
<body>
<div id="box">
<div @click="show1()">
<input type="button" value="按鈕" @click="show($event)">
</div>
</div>
</body>
</html>
<2>利用vue的方法阻止冒泡:給HTML元素綁定click事件的時(shí)候,改為@click.stop="show()"
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8">
<script src="http://unpkg.com/vue/dist/vue.js"></script>
<script type="text/javascript">
window.onload = function(){
var vm = new Vue({
el:'#box',
methods:{
show:function(event){
alert(1);
//event.cancelBubble = true;
},
show1:function(){
alert(2);
}
}
});
}
</script>
</head>
<body>
<div id="box">
<div @click="show1()">
<input type="button" value="按鈕" @click.stop="show()">
</div>
</div>
</body>
</html>
3.默認(rèn)行為
比如contextmenu右鍵菜單
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8">
<!-- // <script src="vue.js"></script> -->
<script src="http://unpkg.com/vue/dist/vue.js"></script>
<script type="text/javascript">
window.onload = function(){
var vm = new Vue({
el:'#box',
methods:{
show:function(){
alert(1);
},
show1:function(){
alert(2);
}
}
});
}
</script>
</head>
<body>
<div id="box">
<input type="button" value="按鈕" @contextmenu="show()">
<input type="button" value="按鈕1" @contextmenu.prevent="show1()">
<p>//按鈕右擊點(diǎn)下去會依次出現(xiàn) 彈窗 1, 還有右擊的默認(rèn)菜單</p>
<p>//按鈕1右擊只出現(xiàn) 彈窗 2</p>
</div>
</body>
</html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue?mounted周期中document.querySelectorAll()獲取不到元素的解決
這篇文章主要介紹了vue?mounted周期中document.querySelectorAll()獲取不到元素的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03
vite獲取所有環(huán)境變量(env)的實(shí)現(xiàn)方法
本文主要介紹了vite獲取所有環(huán)境變量(env)的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-06-06
基于Element-Ui封裝公共表格組件的詳細(xì)圖文步驟
在平時(shí)開發(fā)的時(shí)候很多情況都會使用到表格和分頁功能,下面這篇文章主要給大家介紹了關(guān)于如何基于Element-Ui封裝公共表格組件的詳細(xì)圖文步驟,需要的朋友可以參考下2022-09-09
vue3+ts實(shí)現(xiàn)樹形組件(菜單組件)
本文主要介紹了vue3+ts實(shí)現(xiàn)樹形組件(菜單組件),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05
利用vue+turn.js實(shí)現(xiàn)翻書效果完整實(shí)例
這篇文章主要給大家介紹了關(guān)于利用vue+turn.js實(shí)現(xiàn)翻書效果的相關(guān)資料,turn.js是使用了jquery書寫的,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01
vue中el-select中多選回顯數(shù)據(jù)后沒法重新選擇和更改的解決
本文主要介紹了vue中el-select中多選回顯數(shù)據(jù)后沒法重新選擇和更改解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-01-01
vue-better-scroll 的使用實(shí)例代碼詳解
這篇文章主要介紹了vue-better-scroll 的使用實(shí)例代碼詳解,代碼簡單易懂,非常不錯,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-12-12

