简体   繁体   English

e.preventDefault(); undefined不是一个函数

[英]e.preventDefault(); undefined is not a function

I'm getting the following error that is saying my e.preventDefault(); 我收到以下错误,说我的e.preventDefault(); ---> " e. " undefined is not a function when clicking <button class='url_qry_add' onclick='url_qry_add(this);'> . --->e. ”单击<button class='url_qry_add' onclick='url_qry_add(this);'>时, undefined is not a function The function itself is defined before the end of my </body> and I have only invoked jQuery once. 函数本身是在我的</body>结束之前定义的,我只调用了一次jQuery。

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 . 错误不能与文件的顺序有关,我没有意外地将该函数嵌套在另一个函数或document.ready

What am I doing wrong? 我究竟做错了什么?

<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). 因此,我们有变量“ event ”(事件对象,我们有方法preventDefault等)和“ this ”(当前元素)。 I hope that this explanation will help you understand where we get variable " event ". 我希望这个解释能帮助你理解变量“ 事件 ”的位置。

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

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