jQuery中ajax请求的六种方法(三、五):$.getScript()方法

5.$.getScript()方法

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>jQuery中的ajax基础方法</title>
	</head>
	<script type="text/javascript" src="js/jquery-1.11.3.js"></script>
	<script type="text/javascript">
		$(document).ready(function(){
			$("#btn").click(function(){
				/*
					需求:调用一个外部的js; 
					注意这个getScript方法,外部引入还是需要通过服务器部署,才能运行!
					<跑在Tomcat上面>,在我们需要引入较多的外部的js文件的时候,一次性全部
					加载不合理,并且较慢,所以我们使用使用时加载。
				*/
				$.getScript("js/myTestJS.js", function(){
					say();
				});
			});
		});
	</script>
	<body>
		<div style="width:100%;text-align: center;margin-top: 30px;">
    		<input type="button" value="getScript加载js文件并执行文件中js方法" id="btn">
    	</div>
	</body>
</html>

案例外部JS:

function say() {
	alert("this is other js from others!");
}

猜你喜欢

转载自blog.csdn.net/qq_36791569/article/details/81067774