$.getScript()方法

<!DOCTYPE html>
<meta charset="utf-8">
<html>
<head>
<title>1111</title>
<script src="../scripts/jquery-1.3.1.js" type="text/javascript"></script>
</head>
<body>
<div class="content">
<p><input type="button" id="send" value="加载"/></p>
<div id="resText">
<h4>已有评论:</h4>
</div>
</div>

<script type="text/javascript">
$(function(){
$("#send").click(function() {
$.getScript("test.js");
});
})
</script>
</body>
</html>

加载的test.js文件,代码如下

var comments=[
{
"name" : "黎明",
"msg" : "学生"
},
{
"name" : "小丽",
"msg" : "学生"
},
{
"name" : "大华",
"msg" : "学生"
},
{
"name" : "李丽",
"msg" : "学生"
},
{
"name" : "李静",
"msg" : "学生"
}
];
var html="";
$.each(comments,function(index, comment) {
html +="<div class='comment'><h5>"+comment['name']+"</h5><p>"+comment['msg']+"</p></div>"
});
$("#resText").append(html);

posted @ 2017-04-20 18:15  代码小精灵  阅读(4398)  评论(0编辑  收藏  举报