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

jQuery报错:$(...).find(...).slideDown/slideUp不是函数,图片画廊悬停异常求助

Hey there! Let's work through that TypeError you're hitting with slideDown()/slideUp() — here's what's going on and how to fix it:

What's Causing the Error?

The most likely issue is that your hover event handler isn't properly targeting the <figcaption> element inside each <figure>, or your code is running before the DOM is fully loaded (so jQuery can't find the elements to apply slide methods to). The error message tells us jQuery isn't recognizing slideDown()/slideUp() as valid functions for whatever object you're calling them on — usually that means the selector didn't return a valid jQuery element set.

Fixed Code Example

First, let's make sure your HTML stays intact:

<figure>
  <img src="gallery/reef2.jpg" width="400" height="285" alt="Another beautiful example of the Great Barrier Reef"/>
  <figcaption>Another cracking shot of the reef from Mark. <span>[Photographer: Mark Willams]</span></figcaption>
</figure>

Now here's the corrected jQuery code, with key fixes:

// Wait for the entire DOM to load before running our code
$(document).ready(function() {
  // Hide all figcaptions by default
  $("figcaption").hide();

  // Attach hover behavior to each figure
  $("figure").each(function() {
    $(this).hover(
      // Mouse enter: show the figcaption with slideDown
      function() {
        $(this).find("figcaption").slideDown();
      },
      // Mouse leave: hide the figcaption with slideUp
      function() {
        $(this).find("figcaption").slideUp();
      }
    );
  });
});

Key Fixes Explained

  1. Wrap code in $(document).ready(): This ensures jQuery doesn't try to manipulate elements before they exist in the DOM — a super common source of "element not found" issues.
  2. Target the right element: Using $(this).find("figcaption") makes sure we're only interacting with the caption inside the currently hovered <figure>, not every caption on the page.
  3. Complete the hover callback: The hover() method takes two functions (one for mouse enter, one for mouse leave) — your original code cut off mid-implementation, which would cause syntax or targeting errors.

Additional Checks if You Still See the Error

  • Verify jQuery is loaded first: Make sure your jQuery library reference comes before your custom script in the HTML. For example:
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
    <script src="your-custom-script.js"></script>
    
  • Check for library conflicts: If you're using other JS libraries (like Prototype) that also use $, switch to using jQuery instead of $, or use noConflict mode:
    jQuery.noConflict();
    jQuery(document).ready(function($) {
      // You can use $ safely inside this function
      $("figcaption").hide();
      // ... rest of your code
    });
    
  • Test selectors in the console: Open your browser's dev tools (F12), go to the Console tab, and run $("figcaption"). If it returns an empty array, your selector isn't finding elements — double-check your HTML structure and DOM load timing.

内容的提问来源于stack exchange,提问作者Brittany Danielle Smith

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 09:28:20