Home > Web Front-end > JS Tutorial > How to solve the problem of how to select the child elements of mouseover div in Jquery

How to solve the problem of how to select the child elements of mouseover div in Jquery

黄舟
Release: 2017-06-28 14:00:09
Original
1176 people have browsed it

There are many divs with the same class (assumed to be a), and each div has a span of the same class (assumed to be b). How can I select the span of the current div?
The method I found is

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

but it doesn’t work for me.

<!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>
Copy after login
<!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(&#39;.b&#39;).css({color:&#39;red&#39;}); 
            });
        });
    </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>
Copy after login

Haha, it’s an own goal.
Didn’t you test my writing method? That's right, but I wrote the span outside the div.
That way of writing is indeed not common, I saw it on stackoverflow.

The above is the detailed content of How to solve the problem of how to select the child elements of mouseover div in Jquery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template