jQuery e.preventdefault(); 不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3362481/
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-08-26 15:18:32  来源:igfitidea点击:

e.preventdefault(); not working

jquerysharepoint

提问by Gary

I'm having real trouble getting e.preventDefault();to work.

我真的很难e.preventDefault();去上班。

Here is my code

这是我的代码

$('#ListSnapshot a').live('click', function(e){
    var url = $(this).attr('href') +' #WebPartWPQ2 .ms-listviewtable';
    $('#ListSnapshot').load(url);
    e.preventDefault();
});

Could someone explain what I'm doing wrong, I can see the load function work but then the page redirects to the clicked link which I need to prevent.

有人可以解释我做错了什么,我可以看到加载功能正常工作,但是页面重定向到我需要阻止的点击链接。

I have also tried moving e.preventDefault();to the top of the function, to no avail.

我也尝试移动e.preventDefault();到函数的顶部,但无济于事。

回答by jcsierra.27

I had a similar problem, in which e.preventDefault()would work on some cases, but not on others. It showed no errors, and using try-catchwas not displaying the catch alert. Adding e.stopImmediatePropagation()did the trick, in case it helps anyone (big thanks to wcpro)

我有一个类似的问题,它e.preventDefault()在某些情况下有效,但在其他情况下无效。它没有显示错误,并且 usingtry-catch没有显示catch alert. 添加可以解决问题e.stopImmediatePropagation(),以防它对任何人有帮助(非常感谢 wcpro)

回答by Rod Johnson

i think you may have the following scenerio... at least, this will reproduce the error

我想你可能有以下场景......至少,这会重现错误

you may have an event higher up that is setup for the hover event, that event may be using bind, and even if you call e.preventdefault it will still call the bind first, so you may need to make the one higher up use live instead of bind. then it should work as expected. check this sample.

您可能有一个为悬停事件设置的更高级别的事件,该事件可能正在使用绑定,即使您调用 e.preventdefault 它仍会首先调用绑定,因此您可能需要让更高级别的事件使用实时而不是绑定。那么它应该按预期工作。检查这个样本。

http://jsfiddle.net/rodmjay/mnkq3/

http://jsfiddle.net/rodmjay/mnkq3/

$('div').bind ('click', function(){ // <-- switch this to live and you will see different behavior

    alert('div click');

});

$('a').live('click', function(e){


    alert('a click');

    e.stopImmediatePropagation();


});

回答by dxh

The code you've provided should definitely be working (working example). There's got to be another issue with your code.

您提供的代码肯定可以正常工作(工作示例)。你的代码肯定有另一个问题。

Try placing an alert inside your event handler to make sure that it fires at all. It's possible that your #ListSnapshot aisn't finding anything.

试着将警报事件处理程序中,以确保它激发所有。有可能你#ListSnapshot a没有找到任何东西。

If something else is wrong inside your handler, that causes an exception, that could prevent the javascript from executing all the way to the preventDefaultcall. I don't see what that could be in the code you've provided, tho.

如果您的处理程序内部出现其他问题,则会导致异常,这可能会阻止 javascript 一直执行到preventDefault调用。我看不到您提供的代码中可能包含的内容,但是。

回答by Josh

I think @David Hedlund's answer is correct, there must be an exception happening. When I write event handlers I use a try...catchblock to make sure that the default action doesn't happen. Try this:

我认为@David Hedlund 的回答是正确的,一定有异常发生。当我编写事件处理程序时,我使用一个try...catch块来确保默认操作不会发生。尝试这个:

$('#ListSnapshot a').live('click', function(e){
    try {
        var url = $(this).attr('href') +' #WebPartWPQ2 .ms-listviewtable';
        $('#ListSnapshot').load(url);
    } catch(ex) {
        alert('An error occurred and I need to write some code to handle this!');
    }
    e.preventDefault();
});

That way, since e.preventDefault();is outside the tryblock, even if an error occurs, e.preventDefault();will still be called.

这样,既然e.preventDefault();是在try块之外,即使发生错误,e.preventDefault();仍然会被调用。

回答by pckozub

Have you tried wrapping the event handler in $(document).ready(...) first? Just a thought.

您是否尝试先将事件处理程序包装在 $(document).ready(...) 中?只是一个想法。

回答by designerdre101

e.preventDefault() should execute before the other lines of code in the handler.

e.preventDefault() 应该在处理程序中的其他代码行之前执行。

$('#ListSnapshot a').live('click', function(e){
    e.preventDefault();
    var url = $(this).attr('href') +' #WebPartWPQ2 .ms-listviewtable';
    $('#ListSnapshot').load(url);
});

回答by Teja Kantamneni

Try returning false. livewill not always work the same way as bindworks. check jquery docs.

尝试返回falselive不会总是以与bind作品相同的方式工作。检查 jquery文档

回答by Steward Godwin Jornsen

Old question but this is just the kind of moment should see what firebug logs in the console or returns as error. Something in your code is obviously preventing preventDefault() from prevent a redirect.

老问题,但这只是应该看到萤火虫在控制台中记录什么或作为错误返回的那种时刻。您的代码中的某些内容显然会阻止 preventDefault() 阻止重定向。