tor*_*ard 22 javascript jquery
突然间,今天突然间,我开始在我们网站的每个页面上看到这个
Added non-passive event listener to a scroll-blocking 'touchstart' event.
Consider marking event handler as 'passive' to make the page more responsive
Run Code Online (Sandbox Code Playgroud)
它不仅仅是一两次......它就像成千上万的......
他们疯了.
阻止洪水泛滥的唯一方法就是注释掉这条线
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js" type="text/javascript"></script>
Run Code Online (Sandbox Code Playgroud)
我阅读了其他关于此违规行为的帖子,但我真的看不到我在两小时前和现在之间做了任何不同的事情(我做了一次完全回滚,看它是否有帮助)
它几乎就像有人把一个bug放到了jquery.min.js中,但是我很怀疑那个因为每个人都会得到它.
有任何想法吗?我试着尽我所能调试,我仍然不知道是什么原因导致的?!?
UPDATE
我替换了所有<button><md-tooltip>text</md-tooltip></button>
宽度<button data-toggle="tooltip" title="text"></button>
这删除了99%的所有违规行为.
Ser*_*gio 21
这解决了我的问题:
jQuery.event.special.touchstart = {
setup: function( _, ns, handle ){
if ( ns.includes("noPreventDefault") ) {
this.addEventListener("touchstart", handle, { passive: false });
} else {
this.addEventListener("touchstart", handle, { passive: true });
}
}
};
Run Code Online (Sandbox Code Playgroud)
小智 16
我正在使用各种事件,这似乎解决了我的用例
(function () {
if (typeof EventTarget !== "undefined") {
let func = EventTarget.prototype.addEventListener;
EventTarget.prototype.addEventListener = function (type, fn, capture) {
this.func = func;
if(typeof capture !== "boolean"){
capture = capture || {};
capture.passive = false;
}
this.func(type, fn, capture);
};
};
}());
Run Code Online (Sandbox Code Playgroud)
小智 8
Sergio 的答案是正确的,将其添加到底部的 jquery 脚本中。如果 touchstart 和 touchmove 出现问题,只需添加相同的代码并将 touchstart 替换为 touchmove,如下所示:
jQuery.event.special.touchstart = {
setup: function( _, ns, handle ){
if ( ns.includes("noPreventDefault") ) {
this.addEventListener("touchstart", handle, { passive: false });
} else {
this.addEventListener("touchstart", handle, { passive: true });
}
}
};
jQuery.event.special.touchmove = {
setup: function( _, ns, handle ){
if ( ns.includes("noPreventDefault") ) {
this.addEventListener("touchmove", handle, { passive: false });
} else {
this.addEventListener("touchmove", handle, { passive: true });
}
}
};
Run Code Online (Sandbox Code Playgroud)
@neel-singh 答案的改进解决方案
(function () {
if (typeof EventTarget !== 'undefined') {
let supportsPassive = false;
try {
// Test via a getter in the options object to see if the passive property is accessed
const opts = Object.defineProperty({}, 'passive', {
get: () => {
supportsPassive = true;
},
});
window.addEventListener('testPassive', null, opts);
window.removeEventListener('testPassive', null, opts);
} catch (e) {}
const func = EventTarget.prototype.addEventListener;
EventTarget.prototype.addEventListener = function (type, fn) {
this.func = func;
this.func(type, fn, supportsPassive ? { passive: false } : false);
};
}
})();
Run Code Online (Sandbox Code Playgroud)