How to insert JavaScript upon showing a jQuery ui tab?

随声附和 提交于 2019-12-10 14:14:19

问题


I want to run a JavaScript function on my page, but only when a tab is shown.

Actually have two questions. I am not using ajax tabs, so would I put my JavaScript in the 'load' or 'show' callback, if I only want the code to run once the tab is shown?

Depending on the answer above, what would my code look like to accomplish the following:

I want to have it like this

  • when tab 1 is shown/loaded
  • -->insert JavaScript here
  • when tab2 is shown/loaded
  • -->insert different JavaScript here
  • when tab3 is shown/loaded
  • -->insert different JavaScript here
  • etc.

回答1:


just a quick guess:

$('#foo').tabs({
    show: function(event, ui) {
        if(ui.index == 0)
        {
         //tab 1 is clicked
        }
        else if(ui.index == 1)
        {
         //tab 2 is clicked
        }
        else if(ui.index == 2)
        {
         //tab 3 is clicked
        }
    }
});

You could also use a switch instead of the else ifs :)
Edit: Changed onShow to show as it was changed in jQuery.



来源:https://stackoverflow.com/questions/1521871/how-to-insert-javascript-upon-showing-a-jquery-ui-tab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!