$.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);

时间: 2024-10-09 07:37:18