需在#table加载完成后移除#go_back元素的前端技术问题
Fix: Wait for #table Load to Complete Before Removing #go_back
Hey there, let's break down why your current code isn't working and fix it right away!
The Root Problem
Your code calls $('#table').load() which is an asynchronous operation—it doesn't block the rest of your code from running. That means $('#go_back').remove() fires immediately after starting the load request, not after the table has finished loading into the DOM.
The Solution
jQuery's load() method lets you pass a callback function that runs only after the load operation completes successfully. We just need to move the $('#go_back').remove() line into this callback to ensure it waits for the table to be ready.
Modified Working Code
function create_table() { $.ajax({ method: "GET", url: "/api/data/", success: function(data){ // Pass a callback to load() that runs after the table finishes loading $('#table').load('http://127.0.0.1:8000/results', function() { // This line now executes only when #table is fully loaded $('#go_back').remove(); }); }, error: function(error_data){ console.log("errorrr") console.log(error_data) } }) } document.getElementById("create_table").onclick = function() { create_table(); return false; }
What Changed?
- We added a second argument to
$('#table').load(): an anonymous function that triggers once the content is successfully loaded into the#tableelement. - The
$('#go_back').remove()line is now nested inside this callback, guaranteeing it only runs when the table is fully rendered in the DOM.
内容的提问来源于stack exchange,提问作者barciewicz
相关产品推荐
相关产品推荐

