Vue之监听事件

拜拜、爱过 提交于 2019-12-11 13:29:28
  1. stop   在click后面加一个 .stop, 冒泡到了这里就结束了,就不会冒到上级 @click.stop="doc"
  2. .prevent  @click.prevent="doc"提交事件不再重载页面
  3. .capture  在当前事件上加@click.capture="doc",会让其优先捕捉事件,优先级高于子事件。
  4. .self  @click.self="doc" 只有点击自己,才会导致事件发生
  5. .once @click.once="doc"  事件只能点击一次

 

<style type="text/css">
		   #content{
		       margin: 0 auto;
		       text-align:center;
		       line-height: 40px;
		   }
		   div {
		       width: 100px;
		       cursor:pointer;
		   }
		   #grandFather {
		       background: green;
		   }
		   #father {
		       background: blue;
		   }
		   #me {
		       background: red;
		   }#son {
		        background: gray;
		    }
</style>
                <div id="content">
		    <div id="grandFather"  v-on:click="doc">
		        grandFather
		        <div id="father" v-on:click="doc">
		            father
		            <div id="me" v-on:click="doc">
		                me
		                <div id="son" v-on:click="doc">
		                son
		            </div>
		            </div>
		        </div>
		    </div>
		</div>

 

 

<script type="text/javascript">
	var content = new Vue({
		el:'#content',
		data:{
			id: ''
		},
		methods:{
			doc:function(){
				this.id = event.currentTarget.id;
				alert(this.id);
			}
		}
	})
</script>

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!