jquery hover().addClass() 问题问题、jquery、hover、addClass

2023-09-08 10:02:14 作者:丶随风__丶

http://jsfiddle.net/aBaw6/2/

当您悬停列表项时,此演示不会添加类.

This demo does not add class when you hover a list item.

我在这里做错了什么?

$("li").hover(
  function () {
    $(this).addClass('hover);
  }, 
  function () {
    $(this).removeClass("hover");
  }
);

html

<ul>
    <li>Milk</li>
    <li>Bread</li>
    <li>Chips</li>
    <li>Socks</li>
</ul>

css

.hover{
    color:green;
    font-size: 20px;
}

提前致谢.

推荐答案

您的 JavaScript 格式错误:

Your JavaScript was badly formed:

$("li").hover(
  function () {
    $(this).addClass('hover);
  }, 
  function () {
    $(this).removeClass("hover");
  }
);

应该是:

$("li").hover(
  function () {
    $(this).addClass('hover');
  }, 
  function () {
    $(this).removeClass('hover');
  }
  );

如果您单击屏幕顶部的 JS Lint 按钮,它会告诉您这一点(这不是批评,只是一个注意供您将来使用 JS Fiddle).

If you click on the JS Lint button to the top of the screen it would've told you this (this isn't intended as a criticism, just a note for your future use of JS Fiddle).