使用jQuery隐藏函数获取错误

时间:2012-07-17 15:26:12

标签: jquery html5

我一直试图让这个工作,但它一直给我一个错误。这是jsfiddle:

http://jsfiddle.net/2ZUmM/

希望我不是完全搞砸了。

JS:

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

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>​

3 个答案:

答案 0 :(得分:4)

请记住链接上的return false;

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

答案 1 :(得分:0)

您点击<a>标记,然后点击其链接。您需要阻止浏览器关注链接。

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

答案 2 :(得分:0)

目前,您的链接实际上仍在使用其预期的功能。

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

您希望在/ a链接上return false以防止默认设置,因此链接不会跟随添加到/链接中的网址。