2011-11-03 76 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

相关问题