页面加载时Emoji图片路径失效,需setInterval触发才显示问题求助
Hey there! Let's break down why your emojis only show up when using setInterval but not on initial page load, and how to fix it.
First, let's get to the root of the issue: When you call $('.emojione').Emoji(...) inside $(function()), it runs as soon as the DOM is ready—but if your .emojione elements aren't actually present in the DOM yet (maybe they're loaded dynamically via AJAX, rendered by a framework after the initial DOM ready event, or your plugin script loads after your initialization code), the plugin can't find any elements to process. The setInterval works because by the time it triggers a few seconds later, those elements are available.
Here are actionable fixes to try:
1. Ensure Initialization Runs After Elements Exist
- For static elements: Double-check that your initialization code runs after both the jQuery library and the Emoji plugin script are loaded. If your script tag is placed before these dependencies,
$or.Emoji()might not exist yet, causing silent failures. - For dynamic elements (AJAX/JS-rendered): Call the initialization function right after the elements are added to the DOM. For example, if loading content via AJAX:
$.ajax({ url: '/your-content-endpoint', success: function(data) { $('#content-container').html(data); // Initialize emojis on the newly added content $('.emojione').Emoji({ path:'plugins/lib/img/' }); } });
2. Use MutationObserver for Unpredictable Dynamic Content
If you don't have control over when elements are added (e.g., third-party scripts), use a MutationObserver to watch the DOM and initialize emojis automatically when .emojione elements appear:
$(function() { // Initialize existing elements first $('.emojione').Emoji({ path:'plugins/lib/img/' }); // Set up observer for new elements const observer = new MutationObserver((mutations) => { mutations.forEach(mutation => { // Check for added nodes that are or contain .emojione const newEmojiElements = $(mutation.addedNodes) .find('.emojione') .addBack('.emojione'); if (newEmojiElements.length) { newEmojiElements.Emoji({ path:'plugins/lib/img/' }); } }); }); // Observe the entire body for changes (adjust target if needed) observer.observe(document.body, { childList: true, subtree: true }); });
3. Wait for All Page Resources to Load
Sometimes plugins need more than just DOM readiness—they might rely on images or other resources loading first. Try using window.onload instead of $(function()):
window.onload = function() { $('.emojione').Emoji({ path:'plugins/lib/img/' }); };
4. Add a Short Delay (Quick Sanity Check)
As a fast test, add a tiny delay to ensure everything is ready before initializing:
$(function() { setTimeout(() => { $('.emojione').Emoji({ path:'plugins/lib/img/' }); }, 100); // Adjust delay if needed });
All these fixes replicate the reliability of setInterval without relying on repeated calls—they make sure the plugin only runs when the elements it needs are actually present in the DOM.
内容的提问来源于stack exchange,提问作者nsikak

