Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用jQuery更改下一项的背景?_Jquery_Background_Parent_Next - Fatal编程技术网

如何使用jQuery更改下一项的背景?

如何使用jQuery更改下一项的背景?,jquery,background,parent,next,Jquery,Background,Parent,Next,我有以下的方法,但我不能使它工作。我想删除鼠标上方以下li标记的背景 我的代码: <div id="mainmenu"> <ul> <li><a href="index.php?id=2" >Das Hotel</a></li> <li><a href="index.php?id=3" >Angebote</a></li>

我有以下的方法,但我不能使它工作。我想删除鼠标上方以下li标记的背景

我的代码:

<div id="mainmenu">
  <ul>
          <li><a href="index.php?id=2"  >Das Hotel</a></li>
          <li><a href="index.php?id=3"  >Angebote</a></li>
          <li><a href="index.php?id=4"  >Reservierung</a></li>
          <li><a href="index.php?id=5"  >Kontakt</a></li>
          <li><a href="index.php?id=6"  >Anfahrt</a></li>
          <li><a href="index.php?id=7"  >Rechtliches</a></li>
          <li><a href="index.php?id=8"  >Impressum</a></li>
  </ul>
</div>
这里是小提琴:

JS中有一个错误,所以我必须修复它。我还添加了一个“mouse out”事件处理程序来恢复背景。此外,您将“无”设置为LI的背景,但您的CSS在锚上有背景,而不是LI

以下是JS:

$("#mainmenu ul li a").hover(function () {
   $(this).closest('li').next().find("a").css('background', 'none');
}, function () {
   $(this).closest('li').next().find("a").css('background', '');
});

$(this).parent()是一个
li
。因此,
$(this).parent().next().find(“li”)
将找不到任何内容。同时,您认为需要在鼠标悬停离开后将其更改回去。感谢您的快速响应。我现在做的和你在这里写的一模一样,并将它与小提琴相比较,但在我的页面上它仍然不起作用:这怎么可能?鼠标上方下一个li上的分隔符图形应该隐藏..您有一个javascript错误。您正在访问jquery的$,但它似乎不存在$,这是jquery在“无冲突”模式下运行时的情况。如果将
$(窗口)
更改为
jQuery(窗口)
,您将看到JS错误消失(尽管悬停方法仍然会因为
$
而失败)。我不确定为什么您的jquery库被设置为noConflict模式,但这是我的直觉。
$("#mainmenu ul li a").hover(function () {
  $(this).parent().next('li').find('a').css('background', 'none');
}, function () {
  $(this).parent().next('li').find('a').css('background', '');
});
$("#mainmenu ul li a").hover(function () {
   $(this).closest('li').next().find("a").css('background', 'none');
}, function () {
   $(this).closest('li').next().find("a").css('background', '');
});