讓我們走到這一步的step.Apparently你有一個文件夾一些影片,也有可能分貝的mysql了保存的路徑這些視頻。
首先在您的頁面中加載前4個視頻,使用php等服務器端語言,以防止在您的頁面加載後使用ajax再發出1個請求。
然後因爲你使用jQuery,你可以使用下面的代碼
/* JS */
var totalVideos = 4;
function getImages(){
var request = $.ajax({
url: "getVideos.php",
type: "POST",
data: { last : totalVideos },
dataType: "json"
});
request.done(function(videos) {
renderVideos(videos);
totalVideos += 4;
});
request.fail(function(jqXHR, textStatus) {
alert("Request failed: " + textStatus);
});
}
function renderVideos(vids){
var html = '';
for (var i = vids.length - 1; i >= 0; i--) {
var video = // Create the video html code here based on your needs.
html += video;
}
$('#yourVideoContainer').append(html);
}
$(document).ready(function(){
$('#moreVideos').click(function(){
getImages();
})
})
/* PHP */
if (isset($_POST['last']) && is_numeric($_POST['last'])) {
getVideos(intval($_POST['last']);
}
function getVideos(last) {
global $db; // This is your database conection object assuming it is global.
$query = 'SELECT * FROM videos LIMIT 4 OFFSET :last ORDER BY videos.id DESC';
if ($statement = $db->prepare($query)) {
$statement->bindParam(':last', last);
$statement->execute();
$result = $statement->fetchAll(PDO::FETCH_ASSOC);
if(count($result) > 0){
header('Content-Type: application/json');
echo json_encode($result);
return;
}
}
header("HTTP/1.0 666 Someone stole your vids");
}
如果你不想使用數據庫和服務器端語言 你可以在你的項目中使用一個簡單的JS或JSON文件,所有的 視頻信息將被存儲。然後,只需加載文件通過javascript和 訪問視頻數組或對象。
例如,在JSON文件情況下,你可以這樣做:
$.getJSON("videos.json", function(json) {
console.log(json); // this will show the info it in firebug console
});
來源
2013-09-25 15:04:34
Syd
看一看http://api.jquery.com/load/ –
@LaurentWartel有用的,如果要覆蓋的內容一個div,在這種情況下你仍然需要使用它。 – Kiruse
你有存儲在數據庫中的視頻路徑嗎? – Syd