使用事件侦听器我们应该将 useCapture 设置为 false 以支持旧浏览器,否则它将被用作每个浏览器的默认值
Using event listener should we use useCapture as false to support old browser or it will be used as default for every browser
我正在为一个在不同国家/地区拥有不同客户的组织制作网站。我应该如何使用事件侦听器
document.addEventListener('click', this.handleClickOutsideOption)
或
document.addEventListener('click', this.handleClickOutsideOption, false)
支持大量不同的客户
如docs所述:
Note: useCapture
has not always been optional. Ideally, you should include it for the widest possible browser compatibility.
所以是的,您应该将其设置为 false
以支持大量不同的客户端。
此外,您需要知道什么时候可能要将其设置为 true
。假设您正在单个输入文本框上监听 blur
事件,那么您可以将其保持为 false
,例如:
const blog = document.querySelector('#blog');
blog.addEventListener('blur', function (event) {
// Your logic here...
}, false);
但是,如果您想收听 document
中的所有 blur
事件,则应将其设置为 true
,例如:
document.addEventListener('blur', function (event) {
// Your logic here...
}, true);
因为,将它设置为 true
可以帮助我们利用事件冒泡来处理那些不支持它的事件,例如 focus
、blur
等。基本上它是false
用于大多数使用的事件,因此此参数在现代浏览器中为 optional
,默认为 false
。
我正在为一个在不同国家/地区拥有不同客户的组织制作网站。我应该如何使用事件侦听器
document.addEventListener('click', this.handleClickOutsideOption)
或
document.addEventListener('click', this.handleClickOutsideOption, false)
支持大量不同的客户
如docs所述:
Note:
useCapture
has not always been optional. Ideally, you should include it for the widest possible browser compatibility.
所以是的,您应该将其设置为 false
以支持大量不同的客户端。
此外,您需要知道什么时候可能要将其设置为 true
。假设您正在单个输入文本框上监听 blur
事件,那么您可以将其保持为 false
,例如:
const blog = document.querySelector('#blog');
blog.addEventListener('blur', function (event) {
// Your logic here...
}, false);
但是,如果您想收听 document
中的所有 blur
事件,则应将其设置为 true
,例如:
document.addEventListener('blur', function (event) {
// Your logic here...
}, true);
因为,将它设置为 true
可以帮助我们利用事件冒泡来处理那些不支持它的事件,例如 focus
、blur
等。基本上它是false
用于大多数使用的事件,因此此参数在现代浏览器中为 optional
,默认为 false
。