使用 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>
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
https://zh.javascript.info/dispatch-events#dispatchevent
©xgqfrms 2012-2020
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载