Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.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 - Fatal编程技术网

如何在jquery中选择子级?

如何在jquery中选择子级?,jquery,Jquery,这是示例结构 <div id ="div1"> <div class="div1-child"> <div class="div1-sub-child"> </div> </div> </div> 当我悬停在div1上时,有人能帮助我如何在div1子级上应用jquery效果吗?您不需要jquery来执行此操作。仅使用css就可以让它正常工作。如下- #div1:hove

这是示例结构

<div id ="div1">
    <div class="div1-child">
        <div class="div1-sub-child">
        </div>
    </div>
 </div>


当我悬停在div1上时,有人能帮助我如何在div1子级上应用jquery效果吗?

您不需要jquery来执行此操作。仅使用css就可以让它正常工作。如下-

#div1:hover .div1-sub-child {
   background-color:yellow
}
使用jQuery-

$('#div1').hover({function(){  //this is called whn mouse enters the div
    $(this).find('.div1-sub-child').css('background-color','red'); //your effect here
},function(){   //this is called whn mouse leaves the div
    $(this).find('.div1-sub-child').css('background-color','green'); //your effect here
})
也许像

$(".div1-child").hover(
  function () {
    $(this).find('.div1-sub-child').css(*** your new css ***);
  });
试一试

Hover有两个
回调
使用jquery时,一个将在
Hover
时触发,第二个将在
hoverOut
时触发

 $('#div1').hover({function(){  //this is called whn mouse enters the div
        $(this).find('.div1-sub-child').css('background-color','red'); //your effect here
    },function(){   //this is called whn mouse leaves the div
        $(this).find('.div1-sub-child').css('background-color','green'); //your effect here
  })

要向div添加特殊样式,请尝试:

$("#div1").hover(
  function () {
    $(this).find('div.div1-sub-child').addClass("hover");
  },
  function () {
    $(this).find('div.div1-sub-child').removeClass("hover");
  }
);

在使用jquery和php时经常会出现这种情况。这里有很多方法可以做到这一点,我将添加其中一种。希望这对您有所帮助


$('#div1.div1 child').children().addClass('addClass')

您的函数将选择具有类
div子级的所有元素,这些元素在DOM中的位置为零。使用
$(此)。查找(选择器)
使其与
此相对应
谢谢你们的帮助
$("#div1").hover(
  function () {
    $(this).find('div.div1-sub-child').addClass("hover");
  },
  function () {
    $(this).find('div.div1-sub-child').removeClass("hover");
  }
);