这个问题有一点跟随:
Best way to use Google’s hosted jQuery,but fall back to my hosted library on Google fail
Best way to use Google’s hosted jQuery,but fall back to my hosted library on Google fail
所以我可以检测到JQuery CDN是否关闭并允许这样做:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script> <script type="text/javascript"> if (typeof jQuery == 'undefined') { document.write(unescape("%3Cscript src='/Script/jquery-1.7.2.min.js' type='text/javascript'%3E%3C/script%3E")); } </script>
如何为jquery.validate加载相同的操作:
<script type="text/javascript" language="javascript" src="http://ajax.microsoft.com/ajax/jQuery.Validate/1.6/jQuery.Validate.min.js"></script>
解决方法
改用:
if(typeof $().validate == 'undefined') { /// same code as before except pointing at your local jQuery.validate path }
DEMO – 检查是否存在验证
如果控制台存在,返回true,否则返回false.
删除DEMO中的脚本引用以获取false作为输出.
您的完整代码可能与此类似:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script> <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jQuery.Validate/1.6/jQuery.Validate.min.js"></script> <script type="text/javascript"> if (typeof jQuery == 'undefined') { document.write(unescape("%3Cscript src='/Script/jquery-1.7.2.min.js' type='text/javascript'%3E%3C/script%3E")); }; if (typeof $().validate == 'undefined') { document.write(unescape("%3Cscript src='/Script/jquery.Validate-1.6.min.js' type='text/javascript'%3E%3C/script%3E")); } </script>