Change hash without reload in jQuery

前端 未结 5 1774
耶瑟儿~
耶瑟儿~ 2020-12-04 17:23

I have the following code:

$(\'ul.questions li a\').click(function(event) {
    $(\'.tab\').hide();
    $($(this).attr(\'href\')).fadeIn(\'slow\');
    event         


        
5条回答
  •  情话喂你
    2020-12-04 18:11

    You could try catching the onload event. And stopping the propagation dependent on some flag.

    var changeHash = false;
    
    $('ul.questions li a').click(function(event) {
        var $this = $(this)
        $('.tab').hide();  //you can improve the speed of this selector.
        $($this.attr('href')).fadeIn('slow');
        StopEvent(event);  //notice I've changed this
        changeHash = true;
        window.location.hash = $this.attr('href');
    });
    
    $(window).onload(function(event){
        if (changeHash){
            changeHash = false;
            StopEvent(event);
        }
    }
    
    function StopEvent(event){
        event.preventDefault();
        event.stopPropagation();
        if ($.browser.msie) {
            event.originalEvent.keyCode = 0;
            event.originalEvent.cancelBubble = true;
            event.originalEvent.returnValue = false;
        }
    }
    

    Not tested, so can't say if it would work

提交回复
热议问题