Both event.preventDefault() and event.returnValue = false don't work in Firefox

旧街凉风 提交于 2019-12-12 17:27:50

问题


I have tried these to snippets of codes, the first one works in IE and Chrome, the second one works in Chrome only, but both of them don't work in Firefox. What I want is to stop the page from going to other pages via links

$('.album a').click(function(){
    event.returnValue = false;
    //other codes
})

$('.album a').click(function(){
    event.preventDefault();
    //other codes
})

Edit: This snippet from Ian worked for me

$('.album a').click(function(e){
    e.preventDefault();
    //other codes
});

回答1:


You need to provide the Event parameter:

$('.album a').click(function(e){
    e.preventDefault();
    //other codes
});

You don't need to deal with returnValue, as jQuery normalizes the method to work across browsers, by only calling preventDefault.

Note how the handler in the docs show this eventObject as the parameter passed to it: http://api.jquery.com/click/

And note how the Event object has the preventDefault method: http://api.jquery.com/category/events/event-object/




回答2:


Your callback signature does not register the event argument. Therefor your callbacks do not have access to the event object, and cannot prevent it.

$('.album a').click(function(event){
    event.returnValue = false;
    //other codes
});

$('.album a').click(function(event){
    event.preventDefault();
    //other codes
});



回答3:


Try changing your code to the following:

$('.album a').click(function(e){
    e.preventDefault();
    return false;
})


来源:https://stackoverflow.com/questions/17712796/both-event-preventdefault-and-event-returnvalue-false-dont-work-in-firefox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!