簡體   English   中英

jQuery 選項卡適用於某些但不是所有網站

[英]jQuery tabs working on some but not all websites

我正在嘗試創建一個可以在多個網站上工作的選項卡式表單。 我發現代碼片段在某些頁面上有效,並且選項卡存在且功能正常,但在一個特定網站上,選項卡沒有顯示,並且我收到以下控制台錯誤: Uncaught TypeError: $(...).tabs is not a function 我自己調試這個或在線搜索答案都沒有運氣。 任何幫助,將不勝感激 :)

我有問題的網站: http : //www.jetstar.com/sg/en/home
它似乎可以正常工作的網站: https : //tigerair.com.au/

請注意,下面的代碼片段假定網站上已經存在 jQuery。 我曾嘗試先加載最新版本的 jQuery,但也沒有運氣。

if (typeof($) == 'undefined') {
  var $ = jQuery;
}

function createInvisibleDiv() {
  var invisiblePageSizedDiv = document.createElement("div");
  invisiblePageSizedDiv.setAttribute("id", "pageDiv"); 
  invisiblePageSizedDiv.setAttribute("class", "overlay");
  document.body.appendChild(invisiblePageSizedDiv);
  var style = document.createElement('style');
  style.type = 'text/css';
  style.innerHTML = '.overlay{bottom:0; right:0; left: 0; top:0; overflow:hidden;background-color:rgba(88,88,90,0.8); z-index: 100; position:absolute;}';
  document.body.appendChild(style);
  document.body.style.position = "relative";
}

//this function creates the two tabs
function createUnorderedList(){
    var $unorderedList = $("<ul></ul>");
    $unorderedList.attr("id", "unorderedList"); 
    $unorderedList.attr("class", "ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all")
    for (var i=1; i<3; ++i) {
        var $linkItem = $("<li></li>");
        $linkItem.append(createLink(i));
        $linkItem.attr("class", "ui-state-default ui-corner-top")
        $unorderedList.append($linkItem);
    }
    return $unorderedList;
}

function createLink(i){
    var $link = $("<a></a>");
    if (i==1) {
        $link.attr({"href":'#foo', "title":"Foo"});
        $link.text("Foo");
    } else {
        $link.attr({"href":'#bar', "title":"Bar"});
        $link.text("Bar");
    }
    return $link; 
}

function createFooForm() {
    var $ele = $("<div></div>");
    $ele.attr("id", "foo");
    $ele.text("Thanks for looking at my code");
    return $ele;
}

function createBarForm() {
    var $ele = $("<div></div>");
    $ele.attr("id", "bar");
    $ele.text("I super appreciate it!");
    return $ele;
}

function loadScript(url, callback)
{
    // Adding the script tag to the head
    var head = document.getElementsByTagName('head')[0];
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = url;

    // Then bind the event to the callback function.
    script.onreadystatechange = callback;
    script.onload = callback;

    // Fire the loading
    head.appendChild(script);
}

var generateTabsFunction = function() {
    createInvisibleDiv();
    var $invisibleDiv = $('#pageDiv');
    var $containerDiv = $("<div></div>");
    $containerDiv.attr("id", "containerDiv");

    $containerDiv.append(createUnorderedList());
    $containerDiv.append(createFooForm());
    $containerDiv.append(createBarForm());
    $invisibleDiv.append($containerDiv);
    $('body').append($containerDiv);
    $( "#containerDiv" ).tabs();
}

$("<link/>", {
   rel: "stylesheet",
   type: "text/css",
   href: "https://code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css"
}).appendTo("head");

loadScript("https://code.jquery.com/ui/1.12.1/jquery-ui.js", generateTabsFunction);

聽起來你已經包含了 JQuery,但忘了包含 JQueryUI

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM