我想加载基于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以使该插件工作。请看看上面的小提琴。
如何使用相同的代码加载CSS? – Exception
这是一个完全不同的话题,我想,也许你应该为此打开一个新的问题。你可以看看这里虽然http://www.vidalquevedo.com/how-to-load-css-stylesheets-dynamically-with-jquery –
不工作:-( – Exception