You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

页面加载时Emoji图片路径失效,需setInterval触发才显示问题求助

Fixing Emoji Initialization on Page Load

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:37:16