jQuery标签:如何停止点击跳转?

问题描述:

我添加了一些JQuery选项卡,每当我单击其中的一个时,链接的ID都会附加到url上,并且会跳转到url,这确实很烦人.我一直在寻找一些解决方法,例如设置超时时间和停止回发,但是似乎没有任何效果,有人有什么想法吗?

I have added some JQuery tabs, every time I click on one of them the ID of the link is appended to the url and it jumps to the url, which is really annoying. I have looked for a few fixes like set time out and stopping the postback but nothing seems to work, does anyone have any ideas?

<script type="text/javascript">

      $(document).ready(function () {        

          $('.moo').click(function (evt) {
            // stops from submitting the form
            evt.preventDefault();
            return false;
        });


        $("#tabs").tabs();

        setTimeout(function () {
            if (location.hash) {
                window.scrollTo(0, 0);
            }
        }, 1);

    });

</script>




<div id="tabs">
    <ul>
        <li><a href="#tabs-1" class="moo" onclick="return false;" >Nunc tincidunt</a></li>
        <li><a href="#tabs-2" class="moo" onclick="return false;" >Proin dolor</a></li>
    </ul>

    <div id="tabs-1">
        <uc1:PrizeDrawMiniListControl ID="PrizeDrawMiniListControl1" runat="server" />
    </div>

    <div id="tabs-2">
        <uc2:MostViewedControl ID="MostViewedControl1" runat="server" />
    </div>
</div>

链接没有回发,只是在滚动

The link is not posting back, it's just scrolling

http://jsfiddle.net/3au7K/现在可以正常工作了.

http://jsfiddle.net/3au7K/ its working fine now.

将您的javascript函数更改为此

Change your javascript function to this

$(document).ready(function () {        

      $('.moo').click(function (evt) {
        // stops from submitting the form
        evt.preventDefault();
        return false;
    });


    $("#tabs").tabs();

    // This will work for dynamically added tabs as well

    $("#tabs ul li").delegate('a', 'click', function(e){
         e.preventDefault();
         return false;
    });
});

这个小技巧可以解决问题.这没有什么害处,因为JQuery选项卡在初始化后不需要href属性.

This little hack will do the trick. There is no harm in it because JQuery tabs don't need href attribute after getting initialized.