最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to disable e.preventDefault for some elements - Stack Overflow

programmeradmin3浏览0评论

I am using a OnePage template of bootstrap, I can not click a link, or can not switch a radio button, someone says I am using e.preventDefault()

Open this page /, you will see what I mean when you click "Click me" on that page.

I check the js file, there are lots of e.preventDefault() and I don't know how to modify them.

Is there a way to disable e.preventDefault()?

I want to have some hyperlink to another websites in my OnePage templete, so here is what I am think: I give some particular elements an ID or class, then I write some js, to disable e.perventDefault() only for these elements.

Does anybody know how to do that?

Thanks!

I am using a OnePage template of bootstrap, I can not click a link, or can not switch a radio button, someone says I am using e.preventDefault()

Open this page http://abi.maxinrui./, you will see what I mean when you click "Click me" on that page.

I check the js file, there are lots of e.preventDefault() and I don't know how to modify them.

Is there a way to disable e.preventDefault()?

I want to have some hyperlink to another websites in my OnePage templete, so here is what I am think: I give some particular elements an ID or class, then I write some js, to disable e.perventDefault() only for these elements.

Does anybody know how to do that?

Thanks!

Share Improve this question asked Feb 27, 2014 at 3:30 Xinrui MaXinrui Ma 2,1056 gold badges30 silver badges55 bronze badges 3
  • can you provide your html? – Anoop Joshi P Commented Feb 27, 2014 at 3:31
  • I'm not too sure about this but I believe you can make it return false – Moonhead Commented Feb 27, 2014 at 3:31
  • Sure, the html you can see at abi.maxinrui. – Xinrui Ma Commented Feb 27, 2014 at 3:33
Add a ment  | 

2 Answers 2

Reset to default 3

If you're using jQuery to handle your events, then it's possible!

First, a fiddle (shell for full effect): http://fiddle.jshell/UN5WE/show/
Here's the actual fiddle to edit: http://jsfiddle/UN5WE/

Basically, we're modifying jQuery's Event object, and specifically, the preventDefault method found on the prototype. We maintain a reference to re-enable preventDefault.

EDIT
For your specific use case, here's a way to disable preventDefault (based on a class). Just run this script after jQuery has loaded:

jQuery.Event.prototype.preventDefault = (function(){
    var originalFunc = jQuery.Event.prototype.preventDefault;
    return function(){
        if($(this.target).hasClass('disableDefault')) {return;}
        originalFunc.call(this);
    }
}())

Prior to calling preventDefault, this will check to see if the target has a disableDefault class. If it does, it returns immediately (allowing the default to happen). To test your page, copy that code into your console and then run: $('h1').addClass('disableDefault').

I don't think is possible, or at least not on an easy way that i can think of, you can unbind the handlers if they were setted using bind, but that will also remove any behavior that they have, but you can use a workaround, add a new event handler for your links, i remend that you add a special class to external anchors and then get the href attribute from it and open a new tab using window.open like this:

http://jsfiddle/yV78E/2/

The html

<a href="http://api.jquery./?s=selector" class="externalLink" target="_blank">Hey</a>

The js

// Similar behavior that might be on your site  

$('a').click(function(e){
   e.preventDefault();
    // some code
});

// Use the code below as a workaround

$('.externalLink').click(function(e){
    var targetLink = $(this).attr('href'); 
    window.open(targetLink, '_blank');
});

You only need the second part of the script above, since the first one is just to emulate your problem.

发布评论

评论列表(0)

  1. 暂无评论