Remember which tab was active after refresh

前端 未结 17 2237
眼角桃花
眼角桃花 2020-11-27 04:43

I\'m using jquery tabs on a web page and when the page is refreshed it loses what ever tab I had been on and goes back to the first tab.

Has anyone come across this

17条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-27 05:14

    You can try something like this, it's pretty easy to do.

    # Set selected_tab to the correct index based on the current url anchor hash
    selected_tab = 0
    if matches = /#(\w+)/.exec(window.location.hash)
      # find the index of the tab with the given id
      selected_tab = $('#' + matches[1]).index() - 1
    
    # Initialize the tabs and set 'selected' to equal the selected_tab variable we just set
    $('.tabable').tabs
      selected: selected_tab,  # this is where the magic happens
      select: (evt, ui) ->
        window.location.hash = ui.panel.id  # set an anchor tag in the url with the tab id
    

提交回复
热议问题