Get ID of the element I hover over with jQuery?

后端 未结 4 1806
北海茫月
北海茫月 2020-12-06 04:22

I have a bunch of elements that look like this:

html
php

        
相关标签:
4条回答
  • 2020-12-06 05:05

    These solutions still returned an empty alert for me. For me, following worked when I handled the event object passed to the hover function:

    $(".input-box").hover(function(eventObj) { 
        alert(eventObj.target.id);  
    });
    

    Source of this solution

    0 讨论(0)
  • 2020-12-06 05:13
    $('.tags').hover(
      function() { console.log( 'hovering on' , $(this).attr('id') ); },
      function() {}
    );
    

    Second empty function is for mouse out, you'll probably want to do something on that event as well.

    0 讨论(0)
  • 2020-12-06 05:20

    Use this one:- 
    
        <!DOCTYPE html>
        <html>
        <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
        <script>
        $(document).ready(function(){
            $("p").hover(function(){
            
                //if get onhover id
                alert("NOW GET ON HOVER ID NAME:--"+" "+this.id);
                
                //if get onhover class
                alert("NOW GET ON HOVER CLASS NAME:--"+" "+$(this).attr('class'));
                
                
            });
        });
        </script>
        </head>
        <body>
    
        <p class="getclass" id="get_class_id" >Hover the mouse</p>
    
        </body>
        </html>

    0 讨论(0)
  • 2020-12-06 05:27

    mouseover should do the trick.

    $('.tags').mouseover(function() {
       alert(this.id);
    });
    

    Note that if you want to know when the mouse leaves as well, you can use hover.

    0 讨论(0)
提交回复
热议问题