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();
};
});
}
이
는 다음과 같이 호출됩니다 문에 대한 내부 ".length"를 사용하지 마십시오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하지만 응답에서 혼란을 제거하기 위해 각 요소를 반복해야합니다. –