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

jQuery中fadeToggle/Display触发Uncaught TypeError:功能可用但报错

Fixing the "display is not a function" Error in Your jQuery hoverTiles Function

What's Causing the Error?

The core issue here is super straightforward: jQuery doesn't have a built-in display() method. When you chain $content.fadeToggle(...).display(...), you're trying to call a method that doesn't exist on the jQuery object returned by fadeToggle().

Your functionality still seems to work because the fadeToggle() call runs successfully before the error hits—browsers will halt execution at the line with the error, but the fade animation was already triggered. That said, leaving this error unaddressed is a bad idea (it can cause unexpected behavior later and clutters your console).

How to Fix It

It looks like you're trying to control the display CSS property of your .content element. Instead of the non-existent display() method, use jQuery's built-in css() method. Also, since fadeToggle() is asynchronous, you'll want to adjust the display property after the fade animation finishes using the callback function:

function hoverTiles() {
  $('.content-block').hover(function () {
    var $content = $('.content', this);
    $content.fadeToggle(1000, 'linear', function() {
      // Check if the element is now visible or hidden
      if ($content.is(':visible')) {
        // Set your desired display value (e.g., block, flex, inline-block)
        $content.css('display', 'block');
      } else {
        $content.css('display', 'none');
      }
    });
  });
}

A Quick Note:

If your goal was just to toggle visibility (which fadeToggle() already does), you might not even need the display adjustment at all. fadeToggle() automatically handles setting display: none when hiding and restoring the original display value when showing. The only reason to manually set it is if you need to force a specific display type (like flex instead of block) after the animation.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 09:02:32