Jquery新手在包装函数时遇到问题

Jquery newbie having problems with wrapping function

本文关键字:遇到 问题 函数 包装 新手 Jquery      更新时间:2023-09-26

我几乎没有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

正如MitoxBeyond在评论中指出的那样-您缺少一个结束}。

$.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");}
       } // <------ Missing ending bracket.
  });