2012-05-21 139 views
1

What I want is that: There is a link on top of page. When it is clicked, it should go to in the same page and toggle a div class with jquery.But both of are not working at the same time.. For example:为什么<a href=#A> and jquery click function not work together?

<a href='#A' id='link'>Go to A and toggle a div class</a> 

jquery codes are:

$('#link').click(function() { 
    $('.toggle').toggle(); 
    }); 

    <a name='A'> Here is A </a> 

my codes are more complex than that but same logic with I write here. When I click the link, the div which has toggle class toggles. First it looks jquery codes. But it does not go to #A. That is to say, href attribute is not working. Any idea?...

+2

怎么样在jsfiddle.net – Dhiraj

+0

一个小样本提供一个[的jsfiddle(http://jsfiddle.net)例子吧。 – iambriansreed

+1

这应该工作得很好。现场示例 - http://jsfiddle.net/v8Z53/。 –

回答

3

click() will not fire the default event so you have to set window.location

$('#link').click(function (e) { 
    e.preventDefault(); 
    $('.toggle').toggle(); 
    window.location.hash = ($(e.currentTarget).attr("href")); 
}); 
相关问题