Jquery中的简单测试不起作用。绿色广场没有显示

时间:2011-08-13 06:18:02

标签: javascript jquery html css

好吧,所以当你的鼠标在红色方块上时,绿色正方形会显示,但事实并非如此。这是代码:

 <html>                                                                  
     <head>                                                                  
         <script type="text/javascript" 
            src="jquery-1.6.2.min.js"></script>

         <script type="text/javascript">                                         
         <!--
             $(document).ready(function() {
               $("a").bind("mouseover", 
                function(){
                    $("b").css("display", "block");
               });

               $("a").bind("mouseout", 
                function(){
                    $("b").css("display", "none");
               });
             });
         -->                                   
         </script>                                                               
         </head>                                                                 
     <body>                                                                  
        <div class="a" style="background-color: #ff0000; height: 50px; width: 50px;"></div>
        <div class="b" style="display: none; background-color: #00ff00; height: 50px; width: 50px;"></div>

     </body>                                                                 
 </html>

5 个答案:

答案 0 :(得分:3)

在每个选择器前放置一个点以形成选择器:

$(".a").bind("mouseover", 
                function(){
                ...

您的代码可以简化为:

$(document).ready(function() {
    $(".a").hover(function() {
        $(".b").toggle();
    });
});

http://jsfiddle.net/karim79/EkA6p/1/

答案 1 :(得分:0)

应为$(“。a”)。选择器需要一段时间。如果您有id =“a”,那么您将使用(“#a”),但只需执行$(“a”)将实际选择所有链接,即<a href="">

http://jsfiddle.net/hZL94/

答案 2 :(得分:0)

您需要设置课程.a .b

<script type="text/javascript">                              
     $(document).ready(function() {
        $(".a").bind("mouseover", 
        function(){
            $(".b").css("display", "block");
       });

       $(".a").bind("mouseout", 
        function(){
            $(".b").css("display", "none");
       });
     });                             
</script>  

答案 3 :(得分:0)

这是因为您尝试选择<a><b>元素,而不是<div class="a"><div class="b">元素。 $('.a')代替$('a')。请参阅 jsFiddle

$(document).ready(function() {
    $(".a").bind("mouseover", function() {
        $(".b").css("display", "block");
    });

    $(".a").bind("mouseout", function() {
        $(".b").css("display", "none");
    });
});

答案 4 :(得分:-1)

试试这个

<html>                                                                  
     <head>                                                                  
         <script type="text/javascript" 
            src="http://code.jquery.com/jquery-1.6.2.min.js"></script>

         <script type="text/javascript">                                         
             $(document).ready(function() {
               $("#a").bind("mouseover", 
                function(){
                    $("#b").css("display", "block");
               });

               $("#a").bind("mouseout", 
                function(){
                    $("#b").css("display", "none");
               });
             });                         
         </script>                                                               
         </head>                                                                 
     <body>                                                                  
        <div id="a" style="background-color: #ff0000; height: 50px; width: 50px;"></div>
        <div id="b" style="display: none; background-color: #00ff00; height: 50px; width: 50px;"></div>

     </body>       
     </html>