如何在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");
}
);