簡體   English   中英

頁面加載Javascript之前無法添加到DOM內容

[英]Can't add to DOM content before page load Javascript

我目前正在嘗試從解析后使用javscript將一堆元素添加到列表中。

如果我不添加任何點擊偵聽器,則可以獲取完整列表,但是如果我添加點擊偵聽器,則列表中僅出現前5個項目。

這是我的javascript文件中的代碼,它在html中首先被調用:

if (!Parse.User.current()){
    window.location.href = 'index.html';
}
else {
    getParticipants();
}

$( document ).ready(function() {


$('#sign-out').click(function(){
    Parse.User.logOut();
    window.location.href = 'index.html';
    });

});

function getParticipants (){
var Participants = Parse.Object.extend("Participants");
var query = new Parse.Query(Participants);
query.ascending("patientID");
query.find({
    success: function(results) {
        for (var i = 0; i < results.length; i++) {
            var object = results[i];
            var id = object.get("patientID");
            $(".list-group").append("<li class='list-group-item' id='" + id + "'>" + id + "</li>");
            (function (id) {
                $('#' + object.get("patientID")).click(function (e) {
                    e.preventDefault();

                    if (typeof(Storage) !== "undefined") {
                        localStorage.setItem("patientID", id);
                    } else {
                        // Sorry! No Web Storage support..
                    }

                    window.location = 'profile.html';
                });
            }(id));
        }
    },
    error: function(error) {
        alert("Error: " + error.code + " " + error.message);
    }
});

}

在頁面加載之前,您永遠不能修改DOM,因為那時DOM還沒有准備好。

觸發onload事件后,應執行任何DOM操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM