1

为什么这会拒绝工作?
HTML 的东西

<div id="nav-bar">  
  <ul>  
    <li>  
      <span>  
        <a href="contact.html">Contact</a>  
      </span>  
    </li>  
  </ul>  
</div>

Javascript 的东西

$('div#nav-bar').filter('a').click(function(event){
    event.preventDefault();
});
4

2 回答 2

8

过滤器仅过滤已选择的内容。在你的情况下,#nav-bar元素。

你需要这个:

$('div#nav-bar a').click(function(event){
        event.preventDefault();
    });
于 2012-08-28T15:06:35.493 回答
2

filter is the wrong method to use here. you should either use find to look for elements in a selection:

$('div#nav-bar').find('a')...

or simply combine that into one selector:

$('div#nav-bar a')...

after you've fixed that, your preventDefault will actually get applied and work, theres nothing wrong with that piece of code directly.

于 2012-08-28T15:09:07.927 回答