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

Bootstrap Tooltip在EJS文件中无法生效的问题排查与优化咨询

Troubleshooting Bootstrap Tooltip Not Working in EJS

Hey there! Let's work through why your Bootstrap Tooltip isn't behaving in your EJS file. Here are the most common fixes and checks to try:

1. Verify Script Loading Order

Bootstrap (especially version 4) relies on jQuery and Popper.js to function, so their loading sequence is critical. Make sure your scripts load in this exact order:

<!-- 1. Load jQuery first -->
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
<!-- 2. Then Popper.js -->
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script>
<!-- 3. Next Bootstrap JS -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.min.js"></script>
<!-- 4. Finally your Tooltip activation script -->
<script>
  $(function () {
    $('[data-toggle="tooltip"]').tooltip();
  });
</script>

If you're using Bootstrap 5, note it no longer requires jQuery—but the data attributes have changed (more on that below).

2. Match Bootstrap Version to Your Attributes

This is a super common mistake! Data attributes differ between Bootstrap 4 and 5:

  • Bootstrap 4: Your current data-toggle="tooltip" is correct
  • Bootstrap 5: You need to use data-bs-toggle="tooltip" instead, plus update your activation script:
    <script>
      document.addEventListener('DOMContentLoaded', function() {
        const tooltipTriggers = document.querySelectorAll('[data-bs-toggle="tooltip"]');
        tooltipTriggers.forEach(trigger => new bootstrap.Tooltip(trigger));
      });
    </script>
    

Double-check which Bootstrap version you're loading via CDN—if it's v5, your old attributes won't trigger tooltips.

3. Ensure Elements Exist When Initializing

If your EJS template renders elements dynamically (like inside loops or after AJAX calls), your initial $(function()) might run before those elements are added to the DOM.

For dynamic content, either:

  • Re-run the tooltip initialization code after the dynamic content loads, or
  • Test by moving your activation script to the very bottom of the page, right before the </body> tag.

4. Check Browser Console for Errors

Open your browser's developer tools (F12 → Console tab) and look for red error messages:

  • $ is not defined: jQuery isn't loading correctly
  • Popper is not defined: Popper.js is missing or out of order
  • bootstrap.Tooltip is not a constructor: Wrong Bootstrap version or missing JS file
    These errors will directly point you to the root issue.

5. Test a Simplified Example

To rule out conflicts with your existing HTML/CSS, try a minimal tooltip first:

<button data-toggle="tooltip" title="Test Tooltip">Hover Over Me</button>

If this works, the problem might be with your Font Awesome icon element—try adding display: inline-block to the icon, or moving the tooltip attributes to the parent <li> to test.

Optimized Call Method

For Bootstrap 4, your current initialization code is solid if everything else is set up correctly. For Bootstrap 5, using the native JS approach (no jQuery) is the recommended modern practice—it's lighter and aligns with Bootstrap's current design.

内容的提问来源于stack exchange,提问作者Lj Craft

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 08:36:08