JavaScript:按需加载jQuery [英] JavaScript: Loading jQuery on Demand

查看:71
本文介绍了JavaScript:按需加载jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将jQuery lib动态地包含到页面中的最轻量级方法是什么?
我正在一个页面上工作,有时它运行一些自定义脚本(10行),有时也需要整个jquery。

What is the most lightweight way to include the jQuery lib into a page dynamically? I'm working on a page where sometimes it runs a few custom scripts (10 lines) and other times the entire jquery is also needed.

推荐答案

只需在需要时为jQuery添加脚本标记:

Just add a script tag for jQuery when you need it:

var script = document.createElement('script'); 
script.type = 'text/javascript'; 
script.src = 'http://www.example.com/jquery.js';
document.body.appendChild(script); 

这篇关于JavaScript:按需加载jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆