简体   繁体   English

未捕获的类型错误:e.preventDefault 在传递事件和两个变量时不是 function

[英]Uncaught TypeError: e.preventDefault is not a function when passing event and two variables

I'm beginner in JS, and i thought that i understand event handlers.我是 JS 的初学者,我认为我了解事件处理程序。 Im searching solution for hours, but nothing works.我搜索了几个小时的解决方案,但没有任何效果。 Im trying to do sort of carousel, and it almost works, but when im trying to pass event handler and two variables from one function to another, console saying this: "Uncaught TypeError: e.preventDefault is not a function".我试图做某种轮播,它几乎可以工作,但是当我试图将事件处理程序和两个变量从一个 function 传递到另一个时,控制台说:“未捕获的 TypeError:e.preventDefault 不是函数”。 If somebody would explain me what am i doing wrong i will be very grateful.如果有人能解释我做错了什么,我将不胜感激。 I'm posting my code:我正在发布我的代码:

const boxList = document.querySelectorAll(".second-section .js-s19-item"); 
const firstBoxEl = document.querySelector(".second-section .js-s19-item:first-of-type"); 
const lastBoxEl = document.querySelector(".second-section .js-s19-item:last-of-type"); 
const boxListArr = [...boxList];

function clickRight(e, activeClassNumber, activeClassNumberConstans) {
    e.preventDefault();
    boxList.forEach(el => {
    el.classList.remove("active");
   })

   const slicedArr = boxListArr.slice(activeClassNumber, activeClassNumber+activeClassNumber);
   slicedArr.forEach(el => el.classList.add("active"));
   activeClassNumber = activeClassNumber+activeClassNumber;       
  if(firstBoxEl.classList.contains('active')){
   jQuery(".left").css("display", "none");
   jQuery(".right").css("display", "block"); } else if(lastBoxEl.classList.contains('active')){
 jQuery(".left").css("display", "block");  jQuery(".right").css("display", "none"); }else{ jQuery(".left").css("display", "block"); jQuery(".right").css("display", "block"); }
   }


function clickLeft(e, activeClassNumber, activeClassNumberConstans) {
e.preventDefault();
   boxList.forEach(el => {
    el.classList.remove("active");
   })
      activeClassNumber = activeClassNumber-activeClassNumberConstans;
   const slicedArr = boxListArr.slice(activeClassNumber-activeClassNumberConstans, activeClassNumber);
   slicedArr.forEach(el => el.classList.add("active"));
  if(firstBoxEl.classList.contains('active')){
   jQuery(".left").css("display", "none");
   jQuery(".right").css("display", "block"); } else if(lastBoxEl.classList.contains('active')){
 jQuery(".left").css("display", "block");  jQuery(".right").css("display", "none"); }else{ jQuery(".left").css("display", "block"); jQuery(".right").css("display", "block"); }
}

function handleDesktopChange(e) {
  if (e.matches) {
      let activeElements = document.querySelectorAll(".second-section .js-s19-item:nth-child(-n+3)");
      let activeClass = document.querySelectorAll(".active");
      let activeClassNumber = document.querySelectorAll(".active").length;
      let activeClassNumberConstans = document.querySelectorAll(".active").length;
document.querySelector(".right").addEventListener("click", clickRight(e, activeClassNumber, activeClassNumberConstans));
document.querySelector(".left").addEventListener("click", clickLeft(e, activeClassNumber, activeClassNumberConstans));
      boxList.forEach(el => el.classList.remove("active"));
      activeElements.forEach(el => el.classList.add("active"));

  }
}

mediaQuery.addListener(handleDesktopChange)
handleDesktopChange(mediaQuery) ```

You need to write the following instead:您需要改写以下内容:

document.querySelector(".right").addEventListener("click", e => clickRight(e, activeClassNumber, activeClassNumberConstans));

You're not passing the event as an argument to the clickRight function, it is undefined.您没有将事件作为参数传递给clickRight function,它是未定义的。

What's actually happening with your approach is;您的方法实际发生的情况是; it executes the function, and expects the return of that function to be the event handler.它执行 function,并期望 function 的返回成为事件处理程序。


Going into this a bit further再深入一点

The example you have provided in the comments:您在评论中提供的示例:

const keyChangesSlide = (e) => { 
  if(e.keyCode === 37) { 
    changesSlideLeft(); 
  }
};

window.addEventListener("keydown", keyChangeSlide)

Works completely fine.工作得很好。 Because this translates into the following:因为这转化为以下内容:

window.addEventListener("keydown", (e) => {
  if(e.keyCode === 37) { 
    changesSlideLeft(); 
  }
})

The important part here, is that 1. It is a callback function, and 2. It takes e as a parameter from the callback.这里重要的部分是 1. 它是一个回调 function,以及 2. 它从回调中获取e作为参数。

Your code however, in regard to the example you have provided, would have looked like the following:但是,对于您提供的示例,您的代码将如下所示:

window.addEventListener("keydown", keyChangeSlide(e))

and it would have translated to this:它会翻译成这样:

window.addEventListener("keydown", undefined)

Why?为什么?

Because, you must pass a function as the second parameter of the addEventListener function.因为,您必须传递一个 function 作为addEventListener function 的第二个参数。 However, you are calling a function with a parameter, so what you're ultimately passing is the return value of your function, which in this case is undefined , since you don't return anything.但是,您正在使用参数调用 function,因此您最终传递的是 function 的返回值,在这种情况下是undefined ,因为您不返回任何内容。

And your error occurred, because you passed the argument e , which in that scope is undefined.并且您的错误发生了,因为您传递了参数e ,其中 scope 是未定义的。

change:改变:

document.querySelector(".right").addEventListener("click", clickRight(e, activeClassNumber, activeClassNumberConstans));
document.querySelector(".left").addEventListener("click", clickLeft(e, activeClassNumber, activeClassNumberConstans));

to:至:

document.querySelector(".right").addEventListener("click", e => clickRight(e, activeClassNumber, activeClassNumberConstans));
document.querySelector(".left").addEventListener("click", e => clickLeft(e, activeClassNumber, activeClassNumberConstans));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM