我试图用jQuery查询web服务,并使用jquery模板将回复插入到我的html页面中。我的代码目前看起来像这样:将数据传递给匿名Javascript函数
$(function() {
$('.media-releases h3 div').click(function() {
var button = $(this);
if ($(this).hasClass("expand")) {
$(this).addClass("spinner");
var params = '{ "year": "' + 2012 + '", "month": "' + 02 + '" }';
$.ajax({
type: "POST",
url: "NewsReleases.asmx/GetNewsReleases",
data: params,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var result = jQuery.parseJSON(response.d);
$.get('NewsRelease.htm', function (data) {
// The problem is here, button is null.
// I assume I have to create a closure
// but I'm not sure how to do it.
var ul = button.parentNode.nextSibling;
$.tmpl(data, result).appendTo(ul);
});
button.removeClass("spinner");
button.parents('h3').next().slideDown();
button.removeClass('expand').addClass('minimise');
},
error: function (error) {
/*Error handling code removed*/
}
});
} else {
button.parents('h3').next().slideUp();
button.removeClass('minimise').addClass('expand');
}
});
});
如何使上述函数中的按钮变量可访问,以便我可以将模板附加到它上面?
访问哪里? – Joseph 2012-03-09 08:27:03