Removing event.preventDefault on click doesn't

2019-09-07 08:10发布

问题:

I have this code which prevents default behavior on all elements:

$('body *').click(function(e){
e.stopPropagation();
e.preventDefault();
}); 

Now I would like to programmatically click a certain link in the page but first I have to remove the e.preventDefault(); so I used unbind:

$('a')[0].unbind('click');
$('a')[0].click();

This doesn't work for me. What am I doing wrong?

回答1:

You can't do

$('a')[0].unbind('click')

use .eq() to get the first element and then unbind

.eq(0).unbind('click')