How to show/hide a div on mouseover using jquery?

前端 未结 6 874
别那么骄傲
别那么骄傲 2020-12-12 04:32

I have a table in this format.

div one 2222&
相关标签:
6条回答
  • 2020-12-12 05:02

    you cant give same id to multiple element

    live code is here http://jsfiddle.net/GSz5X/

    <table>
          <tr>
             <td class="divOne">div one</td>
             <td  class="divOne">2222</td>
          </tr>
          <tr>
             <td  class="divOne">div two</td>
             <td  class="divOne">2222</td>
          </tr></div>
    </table>
    
    <div id = "Details" >
    5555shdrhdrh
    </div>
    ​
    
    $(function() {
    $('.divOne').hover(function() { 
        $('#Details').toggle(); 
    });
    });
    ​
    
    0 讨论(0)
  • 2020-12-12 05:12

    First of all you have repeated ids on elements, which is wrong. use classes instead, ids should be unique

    <table>
          <tr>
             <td class="divOne">div one</td>
             <td  class="divOne">2222</td>
          </tr>
          <tr>
             <td  class="divOne">div two</td>
             <td  class="divOne">2222</td>
          </tr>
    </table>
    

    then change your code

    $(function() {
      $('.divOne').hover(function() { 
        $('#Details').show(); 
      }, function() { 
        $('#Details').hide(); 
      });
    });
    

    this will end up (with your actual markup) with the result that your div will be shown when you hover on the table and it will be hidden when you don't

    0 讨论(0)
  • 2020-12-12 05:16

    Change your id's to class identifiers. See example here: http://jsfiddle.net/A7f2p/

    0 讨论(0)
  • 2020-12-12 05:21

    There should only be one ID on the page. Change your ID's for classes and your selector for a class selector:

    <table>
          <tr>
             <td class="divOne">div one</td>
             <td class="divOne">2222</td>
          </tr>
          <tr>
             <td class="divOne">div two</td>
             <td class="divOne">2222</td>
          </tr>
    </table>
    

    And:

    $(function() {
    $('.divOne').hover(function() { 
        $('#Details').show(); 
    }, function() { 
        $('#Details').hide(); 
    });
    });
    
    0 讨论(0)
  • 2020-12-12 05:23

    The id attribute specifies a unique id for an HTML element (the value must be unique within the HTML document).

    <table id="divOne">
      <tr>
         <td>div one</td>
         <td>2222</td>
      </tr>
      <tr>
         <td>div two</td>
         <td>2222</td>
      </tr>
    </table>
    <div id="Details" style="display: none;">
        5555
    </div>
    <script type="text/javascript">
        $(function() {
            $('#divOne td').hover(
                function() { $('#Details').show(); }, 
                function() { $('#Details').hide(); }
            );
        });
    </script>
    
    0 讨论(0)
  • 2020-12-12 05:27

    USE class instead of ID

    $(function() {
    $('.divOne').hover(function() { 
        $('#Details').show(); 
    }, function() { 
        $('#Details').hide(); 
    });
    });
    

    HTML:

    <td class="divOne">div one</td>
    
    0 讨论(0)
提交回复
热议问题