JQuery onclick导航隐藏和显示

时间:2012-04-10 18:27:10

标签: jquery hide show

我想知道如何为jquery nav编写onclick事件

单击“开始”

<a href="#go">Go</a>

它应该在href中查找id,.show().hide()查找同一类的其他项,如class hide。有人给我看代码吗?

2 个答案:

答案 0 :(得分:2)

您首先需要在锚中添加一个类或(最好)一个ID,如下所示:

<a id="clicker" href="#go">Go</a>

不太确定你在问什么,但这就是我认为你的意思。试试这个:

$(document).ready(function() {
  $('#clicker').click(function() {
  //Grab the "href" attribute of the anchor
    var anchor = $(this).attr('href');

  //If you want to hide
    $(anchor).hide();

  //Uncomment if you want to show
    //$(anchor).show();
  });
});

此代码未经测试,但工作。

根据您的问题,这就是我认为您的意思。 ;)

答案 1 :(得分:0)

这可能接近你所要求的,但要确保我们需要更多细节和你尝试过的样本:

HTML:

<a href="#go">Go</a>

<div id="go" style="display:none;">
   Div for Go
</div>

jQuery的:

// the element with the href of "#go"
$("[href='#go']").click(function() {
   // obtain the href value
   var href = $(this).attr("href");

   // find the element with an ID of #go and hide/show it 
   $(href).toggle();
});​

http://jsfiddle.net/ZGz4g/

相关问题