Javascript

use js to detect real user click event All In One

本文主要是介绍use js to detect real user click event All In One,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

use js to detect real user click event All In One

使用 js 检测真实的用户点击事件 All In One

<button id="btn" class="btn">Subscribe</button>

<script>
  btn.addEventListener('click', (event) => {
    console.log('click btn');
    // 有一种方法可以区分“真实”用户事件和通过脚本生成的事件 event.isTrusted
    if(event.isTrusted) {
      console.log('✅ 对于来自真实用户操作的事件,event.isTrusted 属性为 true', event);
    } else {
      console.log('❌ 对于脚本生成的事件,event.isTrusted 属性为 false', event);
    }
  });
  let click = new Event("click");
  btn.dispatchEvent(click);
</script>

<style lang="css">
  .btn {
    width: 10em;
    height: 5ex;
    background-image: linear-gradient(135deg, #f34079 40%, #fc894d);
    border: none;
    border-radius: 5px;
    font-weight: bold;
    color: white;
    cursor: pointer;
  }
  .btn:active {
    box-shadow: 2px 2px 5px #fc894d;
  }
  </style>


CustomEvent

new CustomEvent(type)
new CustomEvent(type, options)

https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/CustomEvent

https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent

new Event(type)
new Event(type, options)

https://developer.mozilla.org/en-US/docs/Web/API/Event/Event

https://developer.mozilla.org/en-US/docs/Web/API/Event

refs

https://zh.javascript.info/dispatch-events#dispatchevent


Flag Counter

©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载

这篇关于use js to detect real user click event All In One的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!