我一直试图调用getMonthDay函数。我传递一个pubDate参数并继续收到错误 - 在ErrorXCopyDisableContinue上中断 this.getMonthDay不是函数
有什么想法吗?
$(document).ready(function(){
alert = console.log;
var ns = {
init : function(){
$.ajax({
url: '/calendar/RSSSyndicator.aspx?type=N&number=15&category=8-0%2c4-0%2c6-0%2c10-0%2c7-0%2c17-0%2c16-0%2c9-0%2c5-0%2c3-0%2c2-0&department=3&numdays=31&ics=Y&rsstitle=Annandale+-+Event+Listing&rssid=11',
success: this.loaded
});
},
loaded: function(data){
// Find item from the RSS document and iterate over reach one.
$(data).find('item').each(function(i, value){
// Set the title and get rid of all chars including and between ()
var title = $(this).find('title').text().replace(/\w+\s+\(.*?\)/, "");
var link = $(this).find('link').text();
var pubDate = $(this).find('pubDate').text();
alert(title);
alert(link);
alert(pubDate);
test = this.getMonthDay(pubDate);
$('#events').append("<p>" + title + "<br/>" + link + "</p>");
});
// var t = items[0].getElementsByTagName('title');
// alert(t[0].firstChild.nodeValue);
},
getMonthDay : function(pubDate){
var d = new Date(pubDate);
d.month = d.getMonth();
var months = ["January", "February", "March", "Thursday", "Friday", "Saturday", "Sunday"];
var month = months[d.month]
var newMonthDay = month + " " + d.getDay();
return newMonthDay;
}
}
ns.init();
});
答案 0 :(得分:1)
这里实际上有两个问题。最直接/本地是this
引用.each()
循环中的<item>
元素,我们可以通过将引用保持在this
之外来修复此问题,像这样:
var self = this;
然后在函数内部使用self.getMonthDay(pubDate);
。其次,在修复之后......即使然后this
不是您想要的,它也会引用您传递给$.ajax()
的options
对象。要解决此问题,您需要context
option来保持this
为ns
,如下所示:
$(document).ready(function(){
alert = console.log;
var ns = {
init : function(){
$.ajax({
url: '/calendar/RSSSyndicator.aspx?type=N&number=15&category=8-0%2c4-0%2c6-0%2c10-0%2c7-0%2c17-0%2c16-0%2c9-0%2c5-0%2c3-0%2c2-0&department=3&numdays=31&ics=Y&rsstitle=Annandale+-+Event+Listing&rssid=11',
context: this, //add this!
success: this.loaded
});
},
loaded: function(data){
var self = this; //maintain a reference to this
$(data).find('item').each(function(i, value){
var title = $(this).find('title').text().replace(/\w+\s+\(.*?\)/, "");
var link = $(this).find('link').text();
var pubDate = $(this).find('pubDate').text();
var test = self.getMonthDay(pubDate);
$('#events').append("<p>" + title + "<br/>" + link + "</p>");
});
},
getMonthDay : function(pubDate){
var d = new Date(pubDate);
d.month = d.getMonth();
var months = ["January", "February", "March", "Thursday", "Friday", "Saturday", "Sunday"];
var month = months[d.month]
var newMonthDay = month + " " + d.getDay();
return newMonthDay;
}
}
ns.init();
});
答案 1 :(得分:0)
您正在传递给this.getMonthDay
的回调中呼叫.each()
。那时,this
是一个完全不同的背景。相反,请考虑以下内容:
loaded: function(data){
var ns = this;
// Find item from the RSS document and iterate over reach one.
$(data).find('item').each(function(i, value){
// Set the title and get rid of all chars including and between ()
var title = $(this).find('title').text().replace(/\w+\s+\(.*?\)/, "");
var link = $(this).find('link').text();
var pubDate = $(this).find('pubDate').text();
alert(title);
alert(link);
alert(pubDate);
test = ns.getMonthDay(pubDate);
$('#events').append("<p>" + title + "<br/>" + link + "</p>");
});
// var t = items[0].getElementsByTagName('title');
// alert(t[0].firstChild.nodeValue);
},
请注意为ns
创建别名(this
),然后可以继续引用该对象,而不管以后可能会使用哪些对象/上下文。