AJAX 函数无法通过滚动工作
2022-08-30 17:12:35
使用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);
?>