禁用除div内链接之外的所有链接的click事件

时间:2023-01-10 15:53:47

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

我有一个页面,在div中有一些链接,有些在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 the all the links except inside the navigation div.

我需要禁用除导航div之外的所有链接的click事件。

How to do it in jquery using similar to:

如何在jquery中使用类似于:

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

3 个解决方案

#1


5  

After a try, the below implementation worked:

经过尝试,以下实现工作:

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

Any flaws in this

这有任何缺陷

#2


2  

This doesn't, in general, sound like a good idea, but here's how I'd do it:

一般来说,这听起来不是一个好主意,但这是我如何做到的:

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

#3


0  

Inspired by K Prime's answer:

灵感来自K Prime的答案:

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

#1


5  

After a try, the below implementation worked:

经过尝试,以下实现工作:

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

Any flaws in this

这有任何缺陷

#2


2  

This doesn't, in general, sound like a good idea, but here's how I'd do it:

一般来说,这听起来不是一个好主意,但这是我如何做到的:

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

#3


0  

Inspired by K Prime's answer:

灵感来自K Prime的答案:

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