更改动态创建的jQuery子元素的样式
我的代码:更改动态创建的jQuery子元素的样式,jquery,css,Jquery,Css,我的代码: $("#posts_cont").on("mouseover",".posts",function (e){ $(this).children(".posts_setting").css("opacity","0.15"); }); $("#posts_cont").on("mouseover",".posts_setting",function (e){ $(this).css("opacity","1"); }); $("#posts_cont").on("mou
$("#posts_cont").on("mouseover",".posts",function (e){
$(this).children(".posts_setting").css("opacity","0.15");
});
$("#posts_cont").on("mouseover",".posts_setting",function (e){
$(this).css("opacity","1");
});
$("#posts_cont").on("mouseout",".posts",function (){
$(this).children(".posts_setting").css("opacity","0");
});
.posts**是一个动态创建的元素,是#posts_cont.的子元素。类似地,.posts_设置是动态创建的**和.posts的子元素。因为有更多的.posts元素,所以为了识别相应的悬停元素,我使用$(this)
posts的mouseover和mouseout可以正常工作并正确更改不透明度。但是.posts_的鼠标悬停设置不会将不透明度更改为1
这个问题有解决办法吗?我需要将.posts\u设置的不透明度在悬停.posts时更改为0.15,在悬停.posts\u设置时更改为1。只需将此添加到css中即可:
#posts_cont .posts_setting:hover {
opacity: 0.15;
}
#posts_cont .posts:hover {
opacity: 1;
}
如果.posts\u设置是.posts的子项,则其不透明度不能高于其父项。在本例中,.posts_设置将设置为比.posts更高的不透明度,因此它将只显示为.posts当前设置为(0.15)的值 解决方案需要更改标记。posts\u设置必须放置在.posts容器外部
如果您可以显示一个小的或更完整的html/css(不管它是静态的,只是您尝试执行的一个示例),我可能会给出一个更具体的解决方案。我将jQuery改为css,正如
@stackErr
CSS:
#posts_cont .posts:hover .posts_setting:hover {
opacity: 1;
}
#posts_cont .posts:hover .posts_setting{
opacity: 0.15;
}
谢谢你的提示。但这并没有节约。我将您的修改为:
#posts_cont.posts:hover.posts设置:hover{opacity:1;}#posts_cont.posts:hover.posts设置{opacity:0.15;}