フィドル: http: //jsfiddle.net/gpTpK/
私が抱えている問題は、$。ajaxが実行されたときにタイトル変数が更新/変更されないことです。行を置き換えようとしたので、ajax呼び出しが機能していることがわかります。
title = $(xml).find("title").text();
と
console.log($(xml).find("title").text());
確かにそれはタイトルを返しますが、元の行を使用する場合、変数のタイトルは変更されません
私は試しましたが、ajax呼び出しを外部に置いても機能します(function($){})(jQuery);
(function($) {
$.fn.getPost = function(options) {
var $this = $(this);
var defaults = {
method: "html",
blogID: "",
postID: "",
done: null
};
var options = $.extend(defaults, options);
var title;
$.ajax({
type: "GET",
url: "http://www.blogger.com/feeds/724793682641096478/posts/default/3551136550258768001",
dataType: "xml",
dataType: 'jsonp',
success: function(xml) {
title = $(xml).find("title").text();
}
});
return $this.each(function() {
if (options.done) {
options.done.call(undefined, title);
}
});
};
})(jQuery);
私は以下を試しました。また、getTitle(){ここにあるajaxコードとreturntitle;}などの関数でajaxをラップしようとしました。
(function($) {
$.fn.getPost = function(options) {
var $this = $(this);
var defaults = {
method: "html",
blogID: "",
postID: "",
done: null
};
var options = $.extend(defaults, options);
var title;
getAjax();
return $this.each(function() {
if (options.done) {
options.done.call(undefined, title);
}
});
function getAjax() {
$.ajax({
type: "GET",
url: "http://www.blogger.com/feeds/724793682641096478/posts/default/3551136550258768001",
dataType: "xml",
dataType: 'jsonp',
async: false,
success: function(xml) {
title = $(xml).find("title").text();
}
});
}
};
})(jQuery);