【问题标题】:jQuery — .not() is not workingjQuery — .not() 不工作
【发布时间】:2011-04-11 00:37:49
【问题描述】:

这是我的代码:

$("a").not(".current_page_item").hover(
   function(){
      $(this).stop().animate(
          {color: "#ffffff"}, 'slow'
      );
   },
   function(){
      $(this).stop().animate(
         {color: "#666666"}, 'slow'
      );
   }
);

如果我特别要求 CSS 更改类,例如:

$(".current_page_item").css("color", "#ff00ff");

我可以改变它的颜色,所以我知道这不是我的错。

有什么想法吗?

【问题讨论】:

  • 什么是 HTML?链接是否有 current_page_item 类?您需要 jQuery UI 来制作彩色动画。它应该可以工作:jsfiddle.net/fkling/arU6e
  • .not() 确实工作。你在某个地方犯了错误。这是你的错。
  • 你能把JS Fiddle demo放在一起吗?
  • 做 $("a.current_page_item").css("color", "#ff00ff");按预期工作?也许 current_page_item 类在父元素上?
  • 您没有解释代码的哪一部分“不起作用”。您的演示 baked-beans.tv/mv 为我提供了动画颜色链接。

标签: jquery hover filtering


【解决方案1】:

根据demo page 中的标记,您必须这样做:

$("a").not(".current_page_item > a").hover(...)

或者:

var selected = $('.current_page_item > a')[0];
$("a").not(selected).hover(...);

(可能更快)

或者只是:

$("li").not(".current_page_item").hover(...)

(如果它也改变了链接的颜色,不确定(链接很特殊;)))

current_page_item 是父类li,而不是链接本身。

您的代码仅过滤具有 current_page_item 类的链接(a 元素),但它们都没有,因此它会选择所有链接。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多