如何使用jquery激活链接


how to inactive a link using jquery

我有jQuery网站在这里。我在ul标签中有4个链接。在这个链接中,我需要激活一个链接,但必须显示。见下文:

<div class="over hide"  id="waterSecondOver">
   <a href="javascript:void(0);" class="current" id="chancingOverView">Overview</a>
   <a href="javascript:void(0);" id="chancingLocalarea">Local Area</a>
   <a href="javascript:void(0);" id="chancingFloorPlan">Floor Plans</a>
   <a href="javascript:void(0);" id="chancingallery"  >Gallery</a>
</div>

我有一个点击功能在上面的链接(除了第四个即画廊)。参见代码

JQR('#waterSecondOver a').click(function() { 
    // my code
});

这里我需要显示画廊链接,但什么都没有发生,当点击画廊(即必须是非活动链接或我说它的功能是只显示链接为"画廊")

我想这很清楚。

JQR('#waterSecondOver a').click(function(event) { 
event.preventDefault();
   });

不要将锚标记中的href设置为javascript:void(0); -这是混乱的,因为它是不必要的重复。使用jQuery,可以在事件回调中使用return false;,以避免默认的单击事件将您带到新页面或当前页面。虽然这是最简单的方法,但并不总是最好的。有关更多信息,请参阅本文:

http://fuelyourcoding.com/jquery-events-stop-misusing-return-false/

但是你绝对不想手动设置href

我将简单地改变锚的类(让我们说class="active"),并且每次onclick检查该"active"类。

为了简单,你可以这样做…

JQR('#waterSecondOver a').click(function(event) { 
    if(JQR(this).attr('id') == "chancingallery"){
        // inactive all links except the one you dont want
      }else{
        // whatever you need....
    }
});