2012-07-17 62 views
0

我一直试图让这个工作,但它不断给我一个错误。这里是的jsfiddle:jQuery隐藏函数获取错误

http://jsfiddle.net/2ZUmM/

但愿我不是完全拧紧这件事。

JS:

​​

HTML:

 <section id="main"> 
     <header> 

     </header> 
     <article> 
      <a id="showIt" href="">Show only one</a> 
     </article> 
     <footer> 

     </footer> 
    </section> 
    <section id="otherObject"> 
     <header> 

     </header> 
     <article> 
      <img src="tdk.jpg"> 
     </article> 
     <footer> 

     </footer> 
    </section> 
    <section id="otherObjectTwo"> 
     <header> 

     </header> 
     <article> 

     </article> 
     <footer> 

     </footer> 
    </section> 
    <section id="otherObject3"> 
     <header> 

     </header> 
     <article> 

     </article> 
     <footer> 

     </footer> 
    </section>​ 

回答

4

记住return false;链接:

$(document).ready(function() { 
    $('#showIt').click(function() { 
     $('#otherObject').hide(); 
     return false; 
    }); 
});​ 
+0

它也值得了解什么'返回false'实际上是:冒泡。与'preventDefault()'相反 – 2012-07-17 15:29:16

0

您点击一个<a>标签,然后按照其链接。您需要阻止浏览器关注该链接。

$(document).ready(function() { 
    $('#showIt').click(function(e) { 
     e.preventDefault(); 
     $('#otherObject').hide(); 
    }); 
});​ 
0

当前您的链接实际上仍在使用其期望功能。

$(document).ready(function() { 
    $('#showIt').click(function() { 
     $('#otherObject').hide(); 
     return false; 
    }); 
});​ 

你要return false的A /链接,以防止违约和由此的链接将不会跟随到URL中的A /链路增加。