老實說,我不認爲這是偉大的,有50個視頻嵌入在網頁 - 無論知名度還是不;只是因爲它們將被瀏覽器處理,儘管不可見。 (如果我錯了,請隨時糾正我,但瀏覽器將會看到並處理整個DOM,並將樣式應用於「隱藏」位)。
Gustavo Straube給出了一個真正的如果你想在DOM中有50個元素,儘管它可能對瀏覽器和帶寬有影響,但是如何做到這一點還是很好的答案。
我可能會沿着解析XML的方向前進,將所有數據存儲爲JSON,然後用HTML中的jQuery從HTML提供的模板框架(如Mustache.js)動態更新DOM。
/* Generate JSON */
$url = "http://www.theURLofmyXML.blah";
$xml = simplexml_load_file($url);
$i = 0;
$json = array();
while ($i < 49) {
$arr['title'] = (string) $xml->query->results->item[$i]->title;
$arr['videoid'] = (string) $xml->query->results->item[$i]->id;
$arr['explanation'] = (string) $xml->query->results->item[$i]->explanation;
$json[] = $arr;
}
echo json_encode($json);
然後,在你的標記有類似下面的,只是初始化你的第一個X視頻 - 在這個例子中10 ..
$(document).ready(function(){
var template = '{{$title}}<br /><iframe width="400" height="225"'
+ 'src="http://www.youtube.com/embed/{{$videoid}}?rel=0&autohide=1&showinfo=0" frameborder="0" allowfullscreen></iframe><br/>'
+ '{{explanation}}<br /><br />';
var html = '';
var i=0;
for(; i<10; i++){
var item = json[i];
html += Mustache.to_html(template, item);
}
$('#videos').html(html); //where #videos is a div to contain your videos
接下來有一個錨(在這個例子中#next )獲得下10個視頻..
$('#next').click(function(){
/* template, i and json are still in scope! */
var j = i+10;
for(; i<j; i++){
var item = json[i];
html += Mustache.to_html(template, item);
}
$('#videos').html(html); //where #videos is a div to contain your videos
});
這樣做的好處是它也很容易做到先前的錨...
$('#prev').click(function(){
/* template, i and json are still in scope! */
var j = i -10;
i -= 20; //10 for the current page, 10 for the start of the previous page
for(; i<j; i++){ //rebuild div content of previous page
var item = json[i];
html += Mustache.to_html(template, item);
}
$('#videos').html(html);
});
只是再次重申,這是一個替代解決方案 - 我認爲它作爲使用JSON比XML更輕量且更靈活,同時也消除了需要一次不使用50個DOM元素的需求。可能有一個原因是你已經選擇了你的實現,但如果我遇到了這個問題,這不是我要實現的實現!
+1儘管我的答案,因爲它以OP的方式回答問題。 –