【发布时间】:2011-02-27 19:55:38
【问题描述】:
我有以下设置:
<div class="parent">
<div class="child">
</div>
<div class="child">
</div>
<div class="child">
</div>
</div>
当鼠标悬停在其中任何一个上时,我正在尝试同时更改所有这些的所有背景颜色。我试过了:
<script type="text/javascript">
$(function() {
$('.parent').hover( function(){
$(this).css('background-color', 'gray');
},
function(){
$(this).css('background-color', 'red');
});
});
</script>
但是,颜色并没有“显示”孩子<div>s。
有没有办法选择“this”的后代。我连续有很多这样的集合,所以我认为我需要使用“this”,所以我没有通过 id 调用每个父母。我在想这样的事情:
<script type="text/javascript">
$(function() {
$('.parent').hover( function(){
$(this "div").css('background-color', 'gray');
},
function(){
$(this "div").css('background-color', 'red');
});
});
</script>
但是,不能完全让它工作 - jquery.com 上的所有示例都使用 id 选择器...没有使用“this”。
非常感谢!
【问题讨论】:
-
也许这就是你要找的东西:api.jquery.com/children.
标签: javascript jquery html css this