【发布时间】:2015-11-17 08:25:22
【问题描述】:
我几乎没有使用 jquery 的经验,但我设法拼凑出完全符合我要求的代码,作为一个直接的 html 文件。但是,我希望它在 Dot Net Nuke 文章中运行,据我所知,我需要将此代码包装在一个函数中以使其工作。
<div id="content"></div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
function parse(document){
$(document).find("Game").each(function(){
$("#content").append(
'<p>'+$(this).attr('SchoolName')+
' '+$(this).attr('Score1')+
', '+$(this).attr('School2Name')+
' '+$(this).attr('Score2')+
'</p>'
);
});
}
$.ajax({
url: 'http://www.website.com/xmlfilehere.xml', // name of file you want to parse
dataType: "xml",
success: parse,
error: function(jqXHR, textStatus, errorThrown){
alert("Error:" + errorThrown);
//error: function(){alert("Error: Something went wrong");}
});
});
</script>
显然,我需要对代码执行此操作以使其在 DNN 中工作:
Getting this function to work is easy – wrap the code in a simple function and pass the jQuery object as a parameter.
(function($) {
//Your jQuery code here
})(jQuery);
我玩过它,但我无法让它工作。有人可以告诉我如何将我的代码包装在一个简单的函数中吗?当我这样做时,我会遇到这样的错误:
SyntaxError: Unexpected token ')'. Expected '}' to end a object literal.
(anonymous function)GetXMLWrapFunction.html:38
GetXMLWrapFunction.html:26
【问题讨论】:
-
意外的 ) 是因为您的错误函数缺少一个结束 }。
标签: javascript jquery xml-parsing