how to add and remove jquery tabs dynamically?

后端 未结 4 1870
广开言路
广开言路 2021-02-07 12:36

I have an aspx page on which I have 2 static jquery tabs.Upon clicking on a button avaiable on one of these tabs,I would like to add a new tab dynamically,which gets its content

4条回答
  •  庸人自扰
    2021-02-07 13:07

    var main = document.getElementById('main');
    var tabber = createMainTab();
    tabber.setAttribute("id","tabber")
    var mainHelper = createTabHelper();
    var testH = createTabHelperElement("Test tab",tabber);
    var testTab = createTab(testH);
    tabber.appendChild(mainHelper);
    
    mainHelper.appendChild(testH);
    tabber.appendChild(testTab);
    
    main.appendChild(tabber);
    $(tabber).tabs();
    
    function createMainTab(){
        var mainDiv = document.createElement("div");
        mainDiv.setAttribute("class","ui-tabs ui-widget ui-widget-content ui-corner-all");
        mainDiv.style.height="100%";
        mainDiv.onk_initialised = false;
        return mainDiv;
    }
    function createTabHelper(){
        var mainUl = document.createElement("ul");
        mainUl.setAttribute("class","ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all");
        return mainUl;
    }
    function createTabHelperElement(name,mainTab){
        var mainLi = document.createElement("li");
        var active = !mainTab.onk_initialised;
        mainTab.onk_initialised=true;
        if(active){
            mainLi.setAttribute("class","ui-state-default ui-corner-top ui-tabs-selected ui-state-active");
        }else{
            mainLi.setAttribute("class","ui-state-default ui-corner-top");
        }
        mainLi.onk_createdActive = active;
        mainLi.onk_id = "tab_"+cache;
        var oLink = document.createElement("a");
        oLink.setAttribute("href","#tab_"+cache);
        oLink.innerHTML = name;
        mainLi.appendChild(oLink);
        cache++;
        return mainLi;
    }
    function createTab(tabHelper){
        var tabDiv = document.createElement("div");
        if(tabHelper.onk_createdActive){
            tabDiv.setAttribute("class","ui-tabs-panel ui-widget-content ui-corner-bottom");
        }else{
            tabDiv.setAttribute("class","ui-tabs-panel ui-widget-content ui-corner-bottom ui-tabs-hide");
        }
        tabDiv.setAttribute("id",tabHelper.onk_id);
        return tabDiv;
    }
    

提交回复
热议问题