调用 e.preventDefault() 时,javascript 错误“e”未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12271822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 15:46:24  来源:igfitidea点击:

javascript error "e" undefined when calling e.preventDefault()

javascriptjqueryobjectundefinedpreventdefault

提问by Chris22

I didn't write this code and I'm having trouble figuring out why I'm getting the following the error at the first e.preventDefault(). I've tried moving that code around within the .clickevent handler, passing it to function(e){}, replacing it with return false, declaring it var e = $(this.href)(don't laugh, I'm trying to learn), I've checked the value being returned in the a hrefand it is returning the correct hash. The video plays, but I'm getting this error when I run the IE debugger. Would someone please tell me how to properly debug and fix this. Thanks

我没有编写此代码,而且我无法弄清楚为什么我在第一个e.preventDefault(). 我尝试在.click事件处理程序中移动该代码,将其传递给function(e){},用 替换它return false,声明它var e = $(this.href)(不要笑,我正在努力学习),我已经检查了在 中返回的值a href,它是返回正确的hash. 视频可以播放,但在运行 IE 调试器时出现此错误。有人请告诉我如何正确调试和解决这个问题。谢谢

enter image description here

在此处输入图片说明

HTML

HTML

 <a href="#video1" class="blueBtn modal" style="width:150px;"><span>Watch Video <img width="10" height="17" src="images/bluebtn.png"></span></a></div>

Javascript

Javascript

// FANCY BOX
$("a.modal").click(function(){
    var inline=$(this).attr('href');
    $.fancybox({
        'transitionIn'      : 'none',
        'transitionOut'     : 'none',
        'href'              : inline,
        'onComplete'        : function(){
            $(inline+' .flowPlayer').videoPlayer();
            $.fancybox.center(true);
        },
        'onClosed'          : function(){loc();}
    });
    e.preventDefault();         
});

$(".print").click(function(e){
    window.open("print-docs/"+$(this).parent().attr('id')+".html","print","width=1,height=1");
    e.preventDefault();
});

function loc(){
    var location=window.location.href;
    var replaceHash=location.replace(document.location.hash,"");
    window.location.assign(replaceHash);
}

回答by raina77ow

Should be

应该

$("a.modal").click(function(e) { // Note the "e" parameter
    // etc
});

... instead, just like in the second click handler. See, both these functions are supplied with the jQuery Event object(a wrapper around native Event object) as a first parameter. But you still have to let JavaScript know how exactly this parameter will be referred to in your function. )

...相反,就像在第二个点击处理程序中一样。看,这两个函数都与jQuery Event 对象(原生Event object的包装器)一起提供作为第一个参数。但是你仍然需要让 JavaScript 知道这个参数在你的函数中是如何被引用的。)

回答by Adam Rackis

You need to add the eparameter yourself:

您需要自己添加e参数:

$("a.modal").click(function(e){  //<------- right there
    var inline=$(this).attr('href');
    $.fancybox({
        'transitionIn'      : 'none',
        'transitionOut'     : 'none',
        'href'              : inline,
        'onComplete'        : function(){
            $(inline+' .flowPlayer').videoPlayer();
            $.fancybox.center(true);
        },
        'onClosed'          : function(){loc();}
    });
    e.preventDefault();         
});

回答by Erwin

You missed the eparameter:

你错过了e参数:

$("a.modal").click(function(e){

   e.preventDefault();  
}