Home >Web Front-end >JS Tutorial >How to solve the problem of how to select the child elements of mouseover div in Jquery
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(...)})
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>
<!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>
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!