e.preventDefault();未定义的不是一个函数。

时间:2022-03-03 15:44:12

I'm getting the following error that is saying my e.preventDefault(); ---> "e." undefined is not a function when clicking <button class='url_qry_add' onclick='url_qry_add(this);'>. The function itself is defined before the end of my </body> and I have only invoked jQuery once.

我得到了下面的错误,这是我的。preventdefault ();---> "e."当点击

The function structure is as follows:

功能结构如下:

var url_qry_add = function ( e ) {
    e.preventDefault();
    ...
};

It used to be:

过去:

$( "ul.url_qry" ).on( "click", "li .url_qry_add", function ( e ) {
    e.preventDefault();
    ...
});

But subsequent buttons added dynamically afterwards were not being picked up.

但是随后动态添加的按钮没有被拾取。

So I've been trying to figure out how to go about it and decided I should try converting the problem function to a named "invokable" function and putting the call in manually with the onclick='..' into the buttons that exist before and after dynamic creation.

因此,我一直在试图弄清楚该如何处理它,并决定我应该尝试将问题函数转换为一个名为“invokable”的函数,并通过onclick='..在动态创建之前和之后的按钮中。

Like I say, the error must be in the way I've created the function or the way I'm calling it. The error can't be to do with the order of files and I have not accidentally nested the function within another function or a document.ready.

就像我说的,错误必须是我创建函数的方式或者我调用它的方式。这个错误不能与文件的顺序有关,我并没有在另一个函数或文档中意外地嵌套这个函数。

What am I doing wrong?

我做错了什么?

1 个解决方案

#1


4  

<button class='url_qry_add' onclick='url_qry_add(event);'>

var url_qry_add = function (e) {
    console.log(typeof e.preventDefault); // function 
};

Update:

更新:

I'll try clarify how it works "internally", when we add attributes to function url_qry_add "inside" it looks like this:

我将尝试在“内部”中说明它是如何工作的,当我们将属性添加到函数url_qry_add“inside”中时,它看起来是这样的:

document.querySelector('.url_qry_add').addEventListener('click', function (event) {
  (function (event) {
    url_qry_add(event, this, $(this));
  }).call(event.target, event);
});

var url_qry_add = function (event, element, $jElement) {
  console.log(event);
  console.log(element);
  console.log($jElement);
};

Hence, we have variable "event" (event object, where we have method preventDefault and so on), and "this" (current element). I hope that this explanation will help you understand where we get variable "event".

因此,我们有变量“event”(事件对象,我们有方法preventDefault等等)和“this”(当前元素)。我希望这个解释能帮助您理解我们在何处得到变量“事件”。

#1


4  

<button class='url_qry_add' onclick='url_qry_add(event);'>

var url_qry_add = function (e) {
    console.log(typeof e.preventDefault); // function 
};

Update:

更新:

I'll try clarify how it works "internally", when we add attributes to function url_qry_add "inside" it looks like this:

我将尝试在“内部”中说明它是如何工作的,当我们将属性添加到函数url_qry_add“inside”中时,它看起来是这样的:

document.querySelector('.url_qry_add').addEventListener('click', function (event) {
  (function (event) {
    url_qry_add(event, this, $(this));
  }).call(event.target, event);
});

var url_qry_add = function (event, element, $jElement) {
  console.log(event);
  console.log(element);
  console.log($jElement);
};

Hence, we have variable "event" (event object, where we have method preventDefault and so on), and "this" (current element). I hope that this explanation will help you understand where we get variable "event".

因此,我们有变量“event”(事件对象,我们有方法preventDefault等等)和“this”(当前元素)。我希望这个解释能帮助您理解我们在何处得到变量“事件”。