简体   繁体   中英

Removing event.preventDefault on click doesn't work

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?

You can't do

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM