首页 > 其他 > 详细

Svelte 中的事件修饰符

时间:2021-09-09 20:44:39      阅读:29      评论:0      收藏:0      [点我收藏+]

正文

React 中没有事件修饰符,Vue 用的说 @click.once 语法,Svelte 用的是 on:click|once 语法。

<script>
  const clickHandler = (foo) => alert(foo + "trigger");
</script>

<!-- once -->
<div>
  <button on:click|once={clickHandler}>只触发一次: once</button>
</div>

<!-- preventDefault -->
<div>
  <!-- 此时 href 不生效 -->
  <a href="http://www.baidu.com/" on:click|preventDefault={clickHandler}>
    阻止默认行为: preventDefault
  </a>
</div>

<!-- capture -->
<div
  on:click|capture={() => {
    clickHandler("father ");
  }}
>
  <button on:click|capture={() => clickHandler("child ")}>
    让事件触发时机改为:capture
  </button>
</div>

<!-- self -->
<div
  on:click|self={() => {
    clickHandler("father ");
  }}
>
  <button on:click={() => clickHandler("child ")}>
    仅当 event.target 是其本身时才触发:self
  </button>
</div>

<style>
  div {
    padding: 30px 15px;
    background-color: #f1f8fd;
    margin-bottom: 15px;
  }
</style>

 

参考

Svelte 中的事件修饰符

原文:https://www.cnblogs.com/aisowe/p/15245520.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!