Disable click event for all links except links inside div

I have a page with some links inside a div, and some outside this div.

  <div id="navigation">
     <ul>
       <li>
         <a href="/Home/Index">Home</a></li>
      <li>
         <a href="/Home/contactus">Contact Us</a></li>
    </ul>
   </div>
<a href="/User/SignIn">Sign In</a>......

I need to disable the click event for all links except inside the navigationdiv.

How to do this in jquery with:

//disable Click event for links
    $("a:not([id])").live('click', function(e) {
        e.preventDefault;
        return false;
    });
+3
source share
3 answers

After an attempted execution, the following work was performed:

 //disable Click event for links except navigation
    $("a:not(#navigation a)").live('click', function(e) {
        e.preventDefault;
        return false;
    });

Any flaws in this

+4
source

This doesn't sound good at all, but here's how I do it:

$('a[href]').live ('click', function (e)
{
    if (!$(this).parents('#navigation').length))
        return false; // same as e.preventDefault() & e.stopPropogation()
});
+2
source

Inspired by K Prime answer:

$('a')
 .filter(function(){return $(this).parents('#navigation').length == 0})
 .live('click', function(e) {
   return false;
 });
0
source

Source: https://habr.com/ru/post/1723844/


All Articles