1
該代碼處理來自RSS源的響應。它組織和附加到內容。如果嵌入了視頻,請將其與其他內容分開。我希望主要關注性能/效率,但我也樂於接受其他建議。那個星形選擇器真的讓我感到困擾,但我不知道有更好的方法來遍歷所有包含的元素。提高Ajax響應處理功能
function getFeed(url, element, callback) {
$.getJSON("https://ajax.googleapis.com/ajax/services/feed/load?v=1.0&callback=?&q="+encodeURIComponent(url), function(response) {
var content = "",
$element = $(element);
for (var i = 0; i < response.responseData.feed.entries.length; i++) {
content = content + response.responseData.feed.entries[i].content; //Join all the feed entries
}
$element.find(".content").html(content).addClass($element.find("embed").length? "withVideo" : "");
$element.find("*").each(function() {
var $this = $(this);
$this.removeAttr("style width align"); //Reset all the crap that comes with the response
if ($this.is("embed")) {
$element.append("<div class='video'></div>");
$this.attr("width", 640).attr("height", 360).parent().appendTo(element + " .video");
};
});
if (typeof callback === 'function') {
callback();
};
});
}
這被當時稱爲像這樣:
getFeed("http://www.kent.k12.wa.us/site/RSS.aspx?PageID=3854", "#TechExpo", optionalCallback);
這裏有一個反應可能是什麼樣子
<div width="500" style="whatever"><p>Some text blah blah blah.</p>
<p align="right">Some more text</p>
</div>
<div><h2>Video Title</h2>
<embed src="http://..." width="360" height="202" type="application/x-shockwave-flash"></embed>
<small>Watch the 7th annual Tech Expo highlights.</small></div>
爲什麼不使用jQuery選擇器(甚至是indexOf)來查找EMBED標記而不是循環遍歷每個節點? –
@Diodeus我會,但我需要循環通過每個元素,以消除響應的混亂。 –