首页 > 解决方案 > 如何为 jquery 1.x 修复 chrome 的非被动元素问题

问题描述

我有一个项目,我们使用带有 dataTables 功能的 jquery 1.x。此功能似乎适用于 Firefox 浏览器,但对于 chrome,它不起作用,也不会在控制台中出现任何错误。它只是发出警告

向阻止滚动的“touchmove”/“touchstart”事件添加了非被动事件侦听器。考虑将事件处理程序标记为“被动”以使页面更具响应性。

我尝试了一些解决方案,例如preventDefault()将脚本添加或添加到被动错误,但没有成功。

喜欢

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 });
      }
   }
};

这与jquery版本控制有关吗?如果是,那么我是否需要将它迁移到数据表(虽然现在它在 Firefox 中得到了支持)。

标签: javascriptjquerydatatable

解决方案


我创建了一个包,passive根据使用情况添加具有适当值的属性preventDefault。请参阅被动事件支持

试一试,确保只传递导致警告的事件:

<script>
  window.passiveEvents = ['touchstart', 'touchmove']
</script>
<script type="text/javascript" src="node_modules/passive-events-support/dist/main.js"></script>

这解决了jquery我的问题。


推荐阅读