简体   繁体   中英

jQuery getting id of hovered element in nested commands

I am looking for something similar to this.id for the hovered element. When I plug this.id into the return... part the id returned is the id container . Any ideas how to jump that and go to the original hovered element's id?

$("#results p").hover(function() {
     $("#container").prepend(function() {
          return "<div id=\"tooltip\"><div id=\"tooltip-inside\">" + e.target.id + "</div></div>";
     });
}, function() {
     $("#tooltip, #tooltip-inside").remove();   
});
$("#results p").hover(function() {
     var hovered = $(this);
     $("#container").prepend('<div id="tooltip"><div id="tooltip-inside">'+hovered.attr('id')+'</div></div>');
}, function() {
     $("#tooltip").remove();   
});

i also improved the code abit.

And if you already using jQuery why not use .attr()

You should read id from target object in hover callback:

$("#results p").hover(function(e) {
     var id = e.target.id;
     $("#container").prepend(function() {
          return "<div id=\"tooltip\"><div id=\"tooltip-inside\">" + id  + "</div></div>";
     });
}, function() {
     $("#tooltip, #tooltip-inside").remove();   
});
$("#results p").hover(function() {
     // get a reference value using jQuery. "this" refers to the element being hovered over
     var myId = $(this).attr('id');
     $("#container").prepend(function() {
          return "<div id=\"tooltip\"><div id=\"tooltip-inside\">" + myId + "</div></div>";
     });
}, function() {
     $("#tooltip, #tooltip-inside").remove();   
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM