jQuery load()无法渲染加载组件内的动态内容,求解决方案
Great question—this is a super common gotcha with jQuery's load() method, so let's break down why it happens and how to fix it.
Why load() isn't picking up dynamic comments
The core issue here is that load() only grabs the static HTML from the target page's #container element. Those user comments that load dynamically? They're probably being added to the page after the initial HTML loads—via an AJAX call to a backend API, or a client-side JS function that renders them once data is available.
load() doesn't run any of the JavaScript that's on the target page (unless you specifically load script tags, but even then, it's limited). So when you yank the #container statically, the code that would normally generate those comments never gets triggered in your current page's context.
Can you make load() work for this?
Absolutely—you just need to add a little extra logic in the load() callback to kick off the dynamic content loading. Here's how:
Option 1: Reuse the target page's dynamic load function
If the target page uses a dedicated function (say, loadUserComments()) to fetch and render the comments, you can call that function manually once load() finishes:
$("#result").load("ajax/test.html #container", function() { // Now that the static container is in place, trigger the comment load loadUserComments(); });
Note: You'll need to make sure this function is accessible in your current page's scope—if it was wrapped in a closure on the target page, you might need to refactor it to be global, or copy the function logic into your current script.
Option 2: Fetch the dynamic content directly (better for performance)
Instead of relying on the target page's JS, cut out the middleman: load the static #container first, then fetch the comment data directly from the source API and render it yourself. This is more efficient because you don't load any extra JS or HTML you don't need:
// Load the static container structure first $("#result").load("ajax/test.html #container", function() { // Fetch comments from your API endpoint $.get("/api/user-comments", function(commentData) { // Render the comments into the loaded container renderComments(commentData, $("#result #container")); }); }); // Example render function (adjust to match your comment structure) function renderComments(comments, $container) { let commentHtml = ""; comments.forEach(comment => { commentHtml += ` <div class="comment"> <h4>${comment.username}</h4> <p>${comment.text}</p> </div> `; }); $container.append(commentHtml); }
Option 3: Load and execute the target page's relevant JS
If the dynamic content logic is too complex to replicate, you can fetch the entire target page, extract the #container, and then run the specific scripts that generate the comments. Be careful with this approach to avoid global JS conflicts:
$.get("ajax/test.html", function(fullPageContent) { // Extract the static #container from the full page const $targetContainer = $(fullPageContent).find("#container"); $("#result").html($targetContainer); // Find and execute only the scripts related to comment loading $(fullPageContent).find("script").each(function() { const scriptSrc = $(this).attr("src"); const scriptText = $(this).text(); // If it's an external script that loads comments, reload it if (scriptSrc && scriptSrc.includes("comments-loader.js")) { $("<script>").attr("src", scriptSrc).appendTo("head"); } // If it's inline script with comment logic, run it else if (scriptText.includes("loadUserComments")) { // Use eval sparingly—only if you trust the source! eval(scriptText); } }); });
⚠️ Heads up: Using eval() can be a security risk if you don't control the target page's content. Whenever possible, refactor the comment logic into a reusable external script instead.
Final Takeaway
load() alone can't handle dynamic content because it doesn't execute the target page's post-load JS. But by combining load() with callbacks to trigger dynamic content loading, or fetching the data directly, you can absolutely get those comments to show up in your #result element.
内容的提问来源于stack exchange,提问作者Choxmi

