简体   繁体   English

jQuery 选项卡适用于某些但不是所有网站

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

I am attempting to create a tabbed form that will work across several websites.我正在尝试创建一个可以在多个网站上工作的选项卡式表单。 I have found that the code snippet works on some pages, and the tabs exist and function properly, but on one particular website the tabs do not display and I get the following console error: Uncaught TypeError: $(...).tabs is not a function .我发现代码片段在某些页面上有效,并且选项卡存在且功能正常,但在一个特定网站上,选项卡没有显示,并且我收到以下控制台错误: Uncaught TypeError: $(...).tabs is not a function I have not had any luck debugging this myself or searching for answers online.我自己调试这个或在线搜索答案都没有运气。 Any help would be appreciated :)任何帮助,将不胜感激 :)

Website I have issues with: http://www.jetstar.com/sg/en/home我有问题的网站: http : //www.jetstar.com/sg/en/home
Website it appears to work fine on: https://tigerair.com.au/它似乎可以正常工作的网站: https : //tigerair.com.au/

Note that the code snippet below assumes jQuery does exist on the website already.请注意,下面的代码片段假定网站上已经存在 jQuery。 I have tried loading the latest version of jQuery first also with no luck.我曾尝试先加载最新版本的 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