Files
freeCodeCamp/guide/chinese/javascript/tutorials/detect-authentic-click-events/index.md
2018-10-16 21:32:40 +05:30

27 lines
830 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: Detect authentic click events
localeTitle: 检测可靠的点击事件
---
## 检测可靠的点击事件
可能存在这样的情况只有当用户真正触发click事件而不是某个脚本来模拟click事件时才想要执行某些特定事情。
这个问题有一个非常简单的解决方案javascript事件对象为我们提供了一个`.istrusted`属性,可以用它来区分。
#### 以下是使用此方法的示例
```javascript
// Assume there is a button in the HTML
const button = document.querySelector('button');
button.addEventListener('click', (e) => {
if (e.isTrusted) {
console.log('Button clicked by a real user');
} else {
console.log('Button click simulated by a script');
}
});
button.click() // Outputs "Button click simulated by a script"
```