2011-11-03 75 views
0

我想加載基於fiddle中顯示的選擇的jQuery插件。
JS爲什麼getScript()在我的情況下不工作

$(document).ready(function() 
{ 
$('img').each(function() 
    { 
     var th=$(this); 
     var className=th.prop('class'); 
     var clas=['type1','type2','type3','type4','type5','type6','type7','type8','type9','type10']; 
     var choice=((jQuery.inArray(className,clas))*1) + 1; 
     var str=""; 
     var src=th.attr('src'); 
     var titleText=th.attr('alt'); 
     switch(choice) 
     { 
      case 1: 
        alert('fall under Type1') 
        str = '<a href="' + src + '" class="cloud-zoom" rel="adjustX: 10, adjustY:-4" target="_blank"/>'; 
        th.wrap(''+str+''); 
        break; 
      case 2: 
        str = '<a href="' + src + '" class="cloud-zoom"'; 
        str+= 'rel="'+"position:'inside', showTitle: false, adjustX:-4, adjustY:-4"+'" target="_blank"/>'; 
        th.wrap(''+str+''); 
        break; 
     } 
     if(choice == 1 || choice == 2) 
     { 
      $("head").append("<link>"); 
      css = $("head").children(":last"); 
      css.attr({ 
      rel: "stylesheet", 
      type: "text/css", 
      href: "http://www.professorcloud.com/styles/cloud-zoom.css" 
      }); 
     // In the js file method is invoking on domReady, That is why i am calling manually again on call back after wrapping HTML 
      $.getScript("http://www.professorcloud.com/js/cloud-zoom.1.0.2.js", function(){ 
       $('.cloud-zoom').CloudZoom(); 
      }); 
     } 
}); 
}); 

HTML

<img src="http://cdn.tripwiremagazine.com/wp-content/uploads/images/stories/Articles/best-jquery/serie3.jpg" class="type4" alt="jghdhefyhe" height="100px" width="130px"> 

在小提琴方法調用domready中下寫入。這可能會導致問題,因爲我正在封裝自定義HTML以使該插件工作。請看看上面的小提琴。

回答

1

我認爲這個問題是,你正試圖從不同的域上的服務器加載腳本。您應該使用$.ajax()並將選項crossDomain設置爲true

$.ajax({ 
    url: 'your url', 
    dataType: "script", 
    crossDomain: true, 
    success: function() 
}); 
+0

如何使用相同的代碼加載CSS? – Exception

+0

這是一個完全不同的話題,我想,也許你應該爲此打開一個新的問題。你可以看看這裏雖然http://www.vidalquevedo.com/how-to-load-css-stylesheets-dynamically-with-jquery –

+0

不工作:-( – Exception

0

此外,您的問題可能是,var src未在您的交換機案例中設置。這產生了一些額外的錯誤。

+0

是的,我發現,當我測試時,但忘了包括新代碼。感謝您的支持。 – Exception

相關問題