2013-05-06 79 views
0

好的,有時jQuery是定義的,有時它不是。以下是我在此頁面上使用的以下jQuery代碼:http://devs.dream-portal.net/dp11/index.phpChrome中的jQuery未定義錯誤

如果定義了列,那麼所有列的寬度都是相同的,否則,2列的列是200像素,中間的寬度是100%。我試圖讓在這裏任何時候都定義jQuery的,但不能找出問題到底是什麼?

任何幫助將極大地意識到:

<script type="text/javascript"> 
if(!window.jQuery) 
{ 
    var script = document.createElement("script"); 
    script.type = "text/javascript"; 
    script.async = false; 
    script.src = "//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"; 
    var oScripts = document.getElementsByTagName("script"); 
    var s = oScripts[0]; 

    s.parentNode.insertBefore(script, s); 
} 

var totaltds = 0; 
function resizeColumns() 
{ 
    jQuery(document).ready(function($){ 
     $('tr[class^="tablerow"]').each(function() { 
      var tds = $(this).children('td[class^="tablecol"]').length; 
      if (tds > totaltds) 
       totaltds = tds; 
     }); 
     var contentPadding = $("#content_section").css('padding-left'); 
     contentPadding = parseInt(contentPadding.substring(0, contentPadding.length - 2)); 
     var subPadding = contentPadding/totaltds; 
     $(window).bind("resize", function(){ 
      for(var i=0; i<totaltds; i++) 
      { 
       var colWidth = ($("#main_content_section").width()/totaltds) - subPadding; 
       $(".tablecol_" + i).css({'width': colWidth + 'px', 'max-width': colWidth + 'px', 'min-width': colWidth + 'px'}); 
      } 

     }).trigger("resize"); 

     $("#hideMe").parent().parent().parent().hide(); 
    }); 
} 

if (document.addEventListener) 
    document.addEventListener("DOMContentLoaded", resizeColumns, false); 
else 
    addLoadEvent(resizeColumns); 

</script> 

<div id="hideMe"></div> 

所有這些代碼插入插入到HTML的主體中,因爲它插入到該頁面上的模塊中,並且需要這樣。我在這裏做錯了什麼?有沒有更好的方法來定義jQuery的,這樣它總是定義:

Chrome的錯誤,幾乎所有的時間在這條線:jQuery(document).ready(function($){

話說jQuery沒有定義。

+1

「所有這些代碼被插入到HTML的主體中,因爲它被插入到該網頁上的模塊,並且需要是這樣的。」這似乎是你的問題。 – j08691 2013-05-06 14:20:36

+0

這個代碼是否在文檔準備好之前包含在頁面中(意思是沒有ajax,而是內聯?) – 2013-05-06 14:21:27

+0

是的,它是內聯的。我之前完成了這個工作,它工作得很好。在定義第一個腳本標記之前,將jQuery插入頭部。 – 2013-05-06 14:23:46

回答

2

試試這個。

如果jQuery未定義,請將其添加到<head>,完成後請致電resizeColumns。 在resizeColumns上,我也添加了noConflict。

由此,未定義將停止,因爲我們將始終等待腳本在嘗試使用之前完全下載。

<script type="text/javascript"> 
// Localize jQuery variable 
var jQuery; 

/******** Load jQuery if not present *********/ 
if(window.jQuery===undefined) { 
     var jQueryTag=document.createElement('script'); 
     jQueryTag.setAttribute("type","text/javascript"); 
     jQueryTag.setAttribute("src", 
     "//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"); 

     if(jQueryTag.readyState) { 
      jQueryTag.onreadystatechange=function() { // For old versions of IE 
       if(this.readyState=='complete'||this.readyState=='loaded') { 
        resizeColumns(); 
       } 
      }; 
     } else { 
      jQueryTag.onload=resizeColumns; 
     } 
     // Try to find the head, otherwise default to the documentElement 
     (document.getElementsByTagName("head")[0]||document.documentElement).appendChild(jQueryTag); 
} else { 
     // The jQuery version on the window is the one we want to use 
     jQuery=window.jQuery; 
     resizeColumns(); 
} 

var totaltds = 0; 
function resizeColumns() 
{ 
    jQuery=window.jQuery.noConflict(true); 
    jQuery(document).ready(function($){ 
     $('tr[class^="tablerow"]').each(function() { 
      var tds = $(this).children('td[class^="tablecol"]').length; 
      if (tds > totaltds) 
       totaltds = tds; 
     }); 
     var contentPadding = $("#content_section").css('padding-left'); 
     contentPadding = parseInt(contentPadding.substring(0, contentPadding.length - 2)); 
     var subPadding = contentPadding/totaltds; 
     $(window).bind("resize", function(){ 
      for(var i=0; i<totaltds; i++) 
      { 
       var colWidth = ($("#main_content_section").width()/totaltds) - subPadding; 
       $(".tablecol_" + i).css({'width': colWidth + 'px', 'max-width': colWidth + 'px', 'min-width': colWidth + 'px'}); 
      } 

     }).trigger("resize"); 

     $("#hideMe").parent().parent().parent().hide(); 
    }); 
} 

/* //Dont need that listener IMO 

if (document.addEventListener) 
    document.addEventListener("DOMContentLoaded", resizeColumns, false); 
else 
    addLoadEvent(resizeColumns);*/ 

</script> 
+0

哇,真棒,非常感謝! – 2013-05-06 15:37:47

0

你就不能換?:一切

// asynchronous function depending on jQuery 
(function($){ 
    // define your shizzle 
    var project = this, 
     resizeColumns = function(){/**/}; 
})(jQuery); 

這也將確保內部this功能$變量。

通過向DOM樹添加異步函數可以實現同樣的效果。

window.project = (function($){ 
    var resizeColumns = function(){/**/}; 
})(jQuery); 

window.project = (function(){ 
    var $ = window.jQuery || {}, 
     resizeColumns = function(){/**/}; 
}); 
+0

我不知道這是否可能。我知道,如果在代碼運行之前在頁面上單獨調用jQuery,就需要這樣做。也就是說,如果jQuery是先前已經加載,如果jQuery的變量都被定義爲比其他的東西'$' – 2013-05-06 21:45:28

+0

第一個例子是一個快捷方式到jQuery的'$ .Deferred'對象,它返回一個是window.load被稱爲承諾。正如你會寫'$(window).load(function(){/ ** /});'看起來非常穩固,我認爲... ^^ – 2013-05-07 20:10:07