2015-10-16 86 views
1

我有一些应该嵌入到页面中的小部件,并且应该可拖动的大小为&。所以目前的方法是检查页面是否已包含jQuery,jQuery.ui和jQuery.ui样式。如果它包含所有这些内容,只需使用它们,否则 - 从我们的服务网站插入它们并进行适当标记。我对它是如何实现有一些疑问,特别是在这里──首先检查jQuery。如果找到,它会尝试查找jQuery.ui样式。它通过它们的名字的子串寻找2个通用类:什么是检查页面是否具有jQuery,jQuery.ui和jQuery.ui.css的最佳方法

for(var i = 0, j = stylesheets.length; i<j; i++){ 
    var classes = stylesheets[i].rules; 
    if(classes){ console.log({classes:classes}); 
     for (var x = 0, y = classes.length; x < y; x++) { 
      if(classes[x].selectorText.indexOf('.ui-draggable')!=-1 
       || classes[x].selectorText.indexOf('.ui-resizable')!=-1) { 
       if(classes[x].cssText){ 
        console.log('found: '+classes[x].cssText); 
        classCount++; 
        if(classCount==2){ 
         break checkStyles; 
        } 
       } 
      } 
     } 
    }else console.log('no classes'); 
} 

这里是要点。我怎么能确定,如果它发现2个类,这意味着有所有必要的类,以保证正确的小部件行为? 或者我应该添加这些样式吗?无论他们出现在网页上?但是如果我这样做,那么它会将样式内容加倍。它看起来像一个困境。那么如果有一个标准的成熟方法?


UPD。重点不是检测jQuery,而是如何最好地管理jQuery.ui样式。


+1

检查jQuery的使用'如果(jQuery的)' – Saar

回答

3

的jQuery增加了一个全球性的 “jQuery的” 对象(也是如此jQuery.ui),所以你可以做这样的事情:

if (typeof jQuery != "undefined") { 
    if (typeof jQuery.ui != "undefined") { 
     // You've got jQuery with UI 
    } else { 
     // You've only got jQuery, no UI 
    } 
} else { 
    // No jQuery 
} 
相关问题