当我尝试对div中动态加载的window.onload = function () {和$(window).load(function() {)执行getelementbyid时得到null
index.html:
<main >
<div id="main-div">
</div>
</main>homepage.html:
<!-- categories Slider -->
<div class="container-fluid">
<div class="site-slider-two px-md-4">
<div class="row slider-two text-center" id="homepageCategories">javascript:
function loadHomePage() {
$("#main-div").load("homepage.html", function () {
}
}
function showCategoriesHomepage(categories) {
window.onload = function () {
homepageCategoriesId = document.getElementById(homepageCategories);
homepageCategoriesId.innerHTML = "";
//For loop that loops true all the categories
for (var i = 0; i < messages.length; i++) {
//Create dynamic li elements with child p elements
var mainDiv = document.createElement('div');
var span = document.createElement('span');
var img = document.createElement('img');
span.setAttribute("class", "border site-btn btn-span");
img.setAttribute("src", categories.image);
//Fills the p elements with user_id, description, created_at
span.appendChild(document.createTextNode(categories.name));
mainDiv.appendChild(img);
mainDiv.appendChild(span);
}
}加载主页后,我调用api,并在完成后尝试获取div上的getelementbyid,但返回null
发布于 2020-02-23 02:19:10
更改此行...
homepageCategoriesId = document.getElementById(homepageCategories);...to this
homepageCategoriesId = document.getElementById('homepageCategories');https://stackoverflow.com/questions/60355164
复制相似问题