用于加载动态 JSON 数据的 JQuery 移动事件

JQuery mobile event for loading dynamic JSON data

本文关键字:JQuery 移动 事件 数据 加载 动态 JSON 用于      更新时间:2023-09-26

我刚刚通读了jQuery Mobile 1.3 API文档事件处理,并且对要使用的适当事件有点迷茫。

基本上,我希望我的应用程序在首次使用 AJAX 启动时加载带有加载映像的博客文章。我只能想到以下事件,但不确定哪个最适合我的需求:

$( document ).on('pagecreate' , '#blogposts', function () {
    $.ajax({
    //Get data from server
    });
});
$( document ).on('pagebeforecreate' , '#blogposts', function () {
    $.ajax({
    //Get data from server
    });
});
$( document ).on('pagebeforeload' , '#blogposts', function () {
    $.ajax({
    //Get data from server
    });
});
$( document ).on('pagebeforeshow' , '#blogposts', function () {
    $.ajax({
    //Get data from server
    });
});
$( document ).on('pagecreate' , '#blogposts', function () {
    $.ajax({
    //Get data from server
    });
});
$( document ).on('pageinit' , '#blogposts', function () {
    $.ajax({
    //Get data from server
    });
});
$( document ).on('pageshow' , '#blogposts', function () {
    $.ajax({
    //Get data from server
    });
});

虽然理论上你可以使用其中任何一个,但通常最好的选择是准备好文档。这样,无论响应时间有多长或多短,只要数据从服务器返回,DOM 都准备好作/注入数据。您可以非常轻松地绑定到此内容:

$(document).ready(function () {
    $.ajax({
        // get data from server
        success: function (data) {
            $('#blogposts').text(data); // or however you want to inject the data
        }
    });
});