老实说,我不认为这是伟大的,有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的方式回答问题。 –