【发布时间】:2015-07-12 04:10:00
【问题描述】:
使用 AJAX 进行无限滚动。内容仅在第一次加载,但不会通过滚动加载。
怎么了?
jQuery:
function loadFeed() {
$.ajax({
url: 'loadmore.php',
dataType: 'html',
success: function (data) {
$("#posts").append('<div class="havanagila"></div>');
$('#posts').html(data);
}
});
}
loadFeed();
$(window).scroll(function () {
var windowScroll = $(window).scrollTop();
var windowHeight = $(window).height();
var documentHeight = $(document).height();
if ((windowScroll + windowHeight) == documentHeight) {
loadFeed();
}
});
加载更多.php:
<?php
session_start();
if ( isset( $_SESSION['login'] ) ) {
$login = $_SESSION['login'];
$id=$_SESSION['id'];
$username="root";
$password="root";
$hostname = "localhost";
$dbname= "kotik";
function testdb_connect ($hostname, $username, $password){
$dbh = new PDO("mysql:host=$hostname;dbname=kotik", $username, $password);
return $dbh;
}
try {
$dbh = testdb_connect ($hostname, $username, $password);
} catch(PDOException $e) {
echo $e->getMessage();
}
}
?>
<?php
$title_select_query= $dbh -> prepare("SELECT title FROM books WHERE id = :id ORDER BY date DESC");
$title_select_query ->execute(array(':id' => $id));
$title_select_query_result = $title_select_query->fetchColumn();
echo($title_select_query_result);
$title_select_query_result = $title_select_query->fetchColumn();
echo($title_select_query_result);
$title_select_query_result = $title_select_query->fetchColumn();
echo($title_select_query_result);
$title_select_query_result = $title_select_query->fetchColumn();
echo($title_select_query_result);
$title_select_query_result = $title_select_query->fetchColumn();
echo($title_select_query_result);
?>
【问题讨论】:
-
您在函数内部有一个事件处理程序,该函数在事件处理程序内部被调用。它只是不断增加。
-
什么不起作用?你检查过
if ((windowScroll + windowHeight) == documentHeight) {这个条件是否真的有效吗? -
@VInand,你能添加预期的 html 结果吗?
-
请查看我的回答并回复
-
他在问为什么不将附加数据添加到 DOM 中。然后,他问为什么要加载相同的数据。我们认为他的 PHP 代码只是为 AJAX 调用提供了示例数据。有人给了他完整的可运行源代码,然后他选择了正确的答案。我永远不会欣赏这种答案,也永远不会支持这种不知道自己想要什么的人。
标签: javascript php jquery ajax pdo