jquery - event.preventDefault() in firefox

2020-05-09 09:32发布

I'm having issues with event.preventDefault() in firefox, it is not working. This is the jquery code.

$("#facebook-del-1").click(function(){
event.preventDefault();
var selector = "#"+$(this).attr("id");
$(selector).closest('a.tag').remove();
});

It is working in Chrome, but not in firefox. You can check out here http://jsfiddle.net/qXPj8/2/

Could anyone guide me?

Thanks!

3条回答
做个烂人
2楼-- · 2020-05-09 09:56

as the other answers have stated, you should pass the "event" argument to the handler method. if you do not want to pass the "event" argument along, you should be able to "return false" at the end of your method and disable default behavior.

查看更多
Luminary・发光体
3楼-- · 2020-05-09 10:07

Pass the event argument to the handler function,

//                                   V-- Added event arg
$("#facebook-del-1").click(function(event) {

Fixed fiddle: http://jsfiddle.net/skram/qXPj8/6/

查看更多
男人必须洒脱
4楼-- · 2020-05-09 10:23

It seems that you have forgotten event argument:

$("#facebook-del-1").click(function(event){
    event.preventDefault();
    var selector = "#"+$(this).attr("id");
    $(selector).closest('a.tag').remove();
});

Please refer to event.preventDefault docs page.

查看更多
登录 后发表回答