当前位置:Gxlcms > JavaScript > Jquery如何选择mouseover的div的子元素的问题解决

Jquery如何选择mouseover的div的子元素的问题解决

时间:2021-07-01 10:21:17 帮助过:12人阅读

有很多class(假设是a)相同的div,每个div下又有相同class(假设是b)的span,如何才能选择当前div的span呢?
查到一种方法是

$(".a").mouseover(function(){$(".b",this).css(...)})

我用来不管用啊。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.2.min.js"></script>
<script type="text/javascript">
$(document).ready( function() {
    $("div.a span.b").mouseover( function() { $(this).css("background-color", "red"); });
});
</script>
</head>
 
<body>
<div class="a"><span class="b">111</span></div>
<div class="a"><span class="b">222</span></div>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="http://code.jquery.com/jquery-1.6.2.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $(".a").mouseover(function () {
                $(this).find('.b').css({color:'red'}); 
            });
        });
    </script>
 
</head>
<body>
<body>
<div class="a" style="border:1px solid red;">
<span class="b">111</span>
<span class="b">222</span>
</div>
<div class="a" style="border:1px solid green;">
<span class="b">333</span>
<span class="b">444</span>
</div>
</body>
 
</body>
</html>

哈哈,日,乌龙了。
你们没有测试一下我的写法吗?也对的,但是我把那个span写在div外边了。
那个写法确实不常见,在stackoverflow上看来的。

以上就是Jquery如何选择mouseover的div的子元素的问题解决的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行