TypeError:event.preventdefault不是函数 [英] TypeError: event.preventdefault is not a function

查看:255
本文介绍了TypeError:event.preventdefault不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我会为此代码获取该代码?
如何正确传递(或不传递)事件?

Why do I get that for this code ? How do I correctly pass (or not) event around?

(原始问题和下面代码的答案是在JavaScript中提供可重用函数的通用逻辑 - 这个问题与方法有关,这个问题是关于事件的问题似乎不同且更具体。)

(original question and the answer with code below was Extracting common logic to a reusable function in JavaScript - that question was about the approach, this question is about the issue with event which seems different and more specific).

$(function(){ 
  var updateUi = function (event) {
       event.preventdefault();
       $("span.show_hide").toggleclass("shown hidden");
       $("table").toggleclass("wide narrow");
  };

  $("a[data-toggle-description-length]='toggle'").click(function(event){
    updateUi(event);
    $.get('/toggle_full_details');
  }); 

  $("a[data-toggle-description-length-refocus]='toggle'").click(function(event){
    updateUi();
    $("input[type='text']:first").focus();   // focus
    $.get('/toggle_full_details');
  });
});

我该如何避免呢?

推荐答案

错字:

由于错字,这是一个真正的类型错误。见下文。

It is a genuine type error because of the Typo. see below.


  • 使用 event.preventDefault(); 代替!

  • use event.preventDefault(); instead!

进一步添加额外的东西:event.preventDefault()不是jQuery的东西。这是一个DOM事件模型的东西。从现在开始,我将把jQuery留在板凳上,因为它在内部处理事件的方式略有不同,所以如果我们可以互换地引用这两个事件,它只会引起混淆和不正确的信息。

Further to add extra: event.preventDefault() isn’t a jQuery thing. It’s a DOM Event Model thing. From this point forward, I’ll be leaving jQuery on the bench, as it handles events slightly differently internally, so it’d just cause confusion and incorrect information if we kept referring to the two interchangeably.

  • http://www.w3.org/TR/2006/WD-DOM-Level-3-Events-20060413/events.html#Events-Event-preventDefault

preventDefault 这可以是评论,但为什么不回答。 :)

preventDefault this can be comment but why not an answer . :)

  • http://api.jquery.com/event.preventdefault/

如果调用此方法,则事件的默认操作将不会是
触发。

If this method is called, the default action of the event will not be triggered.

这篇关于TypeError:event.preventdefault不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆