簡體   English   中英

如何在JqueryWidget代碼中向腳本添加第二個鏈接

[英]How to add second link to script in JqueryWidget code

我已經編寫了一個小部件,並且效果很好,但是現在,我為用戶提供了一個指向遠程腳本的鏈接,一個指向我的腳本的鏈接以及一個放置在其頁面上的div。 我想將其簡化為到我的腳本和div的鏈接。 我想在下面的代碼中包含的遠程鏈接是

<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

有人可以教我如何在代碼中添加第二個鏈接嗎? 我已經鏈接到遠程jquery和遠程樣式表,但是我不確定如何/在何處包含另一個遠程鏈接。 我嘗試了很多地方和方法,但是我一直在打破我的頁面,哈哈。 感謝您提供的任何幫助。

(function() {
    // Localize jQuery variable
    var jQuery;
    /******** Load jQuery if not present *********/
    if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.12.4') {
        var script_tag = document.createElement('script');
        script_tag.setAttribute("type", "text/javascript");
        script_tag.setAttribute("src", "//code.jquery.com/jquery-1.12.4.js");
        if (script_tag.readyState) {
            script_tag.onreadystatechange = function() { // For old versions of IE
                if (this.readyState == 'complete' || this.readyState == 'loaded') {
                    scriptLoadHandler();
                }
            };
        } else {
            script_tag.onload = scriptLoadHandler;
        }
        // Try to find the head, otherwise default to the documentElement
        (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
    } else {
        // The jQuery version on the window is the one we want to use
        jQuery = window.jQuery;
        main();
    }
    /******** Called once jQuery has loaded ******/
    function scriptLoadHandler() {
            // Restore $ and window.jQuery to their previous values and store the
            // new jQuery in our local jQuery variable
            jQuery = window.jQuery.noConflict(true);
            // Call our main function
            main();
        }
        /******** Our main function ********/
    function main() {
        jQuery(document).ready(function($) {
            /******* Load CSS *******/
            var css_link = $("<link>", {
                rel: "stylesheet",
                type: "text/css",
                href: "//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"
            });
            css_link.appendTo('head');
            /******* Load HTML *******/
            var jsonURL = "//www.myurl.com/mssql.php/ws_nfy";
            jQuery(document).ready(function() {
                jQuery.ajax({
                    url: jsonURL,
                    success: searchCallback
                });
            });
            function searchCallback(data) {
                var ws_nfy = data.ws_nfy.records;
                jQuery.each(ws_nfy, function(index, nfy) {
                    jQuery("#tabs ul").append('<li><a href="#tabs-' + nfy[0] + '">' + nfy[2] + '</a></li>');
                    jQuery("#tabs ul").after("<div id='tabs-" + nfy[0] + "'>" + nfy[1] + "</div>");
                });
                $("#tabs").tabs();
            };
        });
    }
})(); // We call our anonymous function immediately

您正在注入jQuery之前檢查它是否已經加載。這很好。 現在,您應該對jQuery-UI執行相同的操作。

這就是相同的過程...

嘗試這個:

(function() {
  // Localize jQuery variable
  var jQuery;
  /******** Load jQuery if not present *********/
  if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.12.4') {
    var script_tag = document.createElement('script');
    script_tag.setAttribute("type", "text/javascript");
    script_tag.setAttribute("src", "//code.jquery.com/jquery-1.12.4.js");
    if (script_tag.readyState) {
      script_tag.onreadystatechange = function() { 
        // For old versions of IE
        if (this.readyState == 'complete' || this.readyState == 'loaded') {
          scriptLoadHandler();
        }
      };
    } else {
      script_tag.onload = scriptLoadHandler;
    }
    // Try to find the head, otherwise default to the documentElement
    (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
  } else {
    // The jQuery version on the window is the one we want to use
    jQuery = window.jQuery;
    //main();                   // Not now! Check for UI first.
    checkUI();
  }
  /******** Called once jQuery has loaded ******/
  function scriptLoadHandler() {
    // Restore $ and window.jQuery to their previous values and store the
    // new jQuery in our local jQuery variable
    jQuery = window.jQuery.noConflict(true);
    // Call our main function
    //main();               // Not now! Check for UI first.
    checkUI();
  }

        // ========================== Check if jQuery-UI is already loaded    
  function checkUI(){
    if(typeof(jQuery.ui) != "undefined"){
      // UI is loaded already.
      console.log("UI is defined");
      console.log( typeof(jQuery.ui) );
      main();

    }else{
      // UI is not loaded. Got to load it.
      console.log("UI is NOT defined");
      //console.log( typeof(jQuery.ui) );

      var ui = document.createElement('script');
      ui.setAttribute("type", "text/javascript");

      // For UI
      window.$ = jQuery;

      ui.setAttribute("src", "https://code.jquery.com/ui/1.12.1/jquery-ui.js");
      console.log(ui);



      document.getElementsByTagName("head")[0].appendChild(ui);

      if (ui.readyState) {
        console.log( "READYSTATE" );
        ui.onreadystatechange = function() { // For old versions of IE
          if (this.readyState == 'complete' || this.readyState == 'loaded') {
            console.log( "STATELOADED" );
            main();
          }
        };
      } else {
        console.log( "ELSE" );
        jQuery(ui).on("load", function(){
          console.log("UI loaded...");
          main();
        });
      }
    }
  }



  /******** Our main function ********/
  function main() {

    jQuery(document).ready(function($) {

      console.log("jQuery: "+jQuery.fn.jquery+"\njQuery-UI: "+jQuery.ui);
      console.log("Tabs element: "+jQuery("#tabs").length);
      /******* Load CSS *******/
      var css_link = $("<link>", {
        rel: "stylesheet",
        type: "text/css",
        href: "//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"
      });
      css_link.appendTo('head');
      /******* Load HTML *******/
      var jsonURL = "//www.myurl.com/api.php/ws_nfy";
      jQuery.ajax({
        url: jsonURL,
        success: searchCallback
      });
      function searchCallback(data) {
        var ws_nfy = data.ws_nfy.records;
        jQuery.each(ws_nfy, function(index, nfy) {
          jQuery("#tabs ul").append('<li><a href="#tabs-' + nfy[0] + '">' + nfy[2] + '</a></li>');
          jQuery("#tabs ul").after("<div id='tabs-" + nfy[0] + "'>" + nfy[1] + "</div>");
        });
        jQuery("#tabs").tabs();
      };
    });
  }
})(); // We call our anonymous function immediately

暫無
暫無

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

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