javascript - jQuery Tooltip positioning issues - Stack Overflow

HTML:<a href="#" rel="tooltip-1">Open Tooltip<a><div id="tool

HTML:

<a href="#" rel="tooltip-1">Open Tooltip</a>
<div id="tooltip-1">Tooltip Content</div>

jQuery:

xOffset = $('#tooltip-1').height() + 10;
yOffset = -30 ;  
$("a[rel=tooltip-1]").hover(function(e){             
 this.t = this.attr("href");
 $("body").append("<p id='tooltip'>"+ this.t +"</p>");
 $("#tooltip")
  .css("top",(e.pageY - xOffset) + "px")
  .css("left",(e.pageX + yOffset) + "px")
  .fadeIn("fast");  
},
function(){
 this.t = "";
 $("#tooltip").remove();
}); 
$("a[rel=tooltip-1]").mousemove(function(e){
 $("#tooltip-1")
  .css("top",(e.pageY - xOffset) + "px")
  .css("left",(e.pageX + yOffset) + "px");
});

Two Issues:

  1. Tooltip div (#tooltip-1) doesn't hide on mouseout.
  2. Since the tooltip div will always have the same ID as REL, I'd like to modify the above jQuery so that it takes target DIV ID automatically.

Thanks in advance for your help.

HTML:

<a href="#" rel="tooltip-1">Open Tooltip</a>
<div id="tooltip-1">Tooltip Content</div>

jQuery:

xOffset = $('#tooltip-1').height() + 10;
yOffset = -30 ;  
$("a[rel=tooltip-1]").hover(function(e){             
 this.t = this.attr("href");
 $("body").append("<p id='tooltip'>"+ this.t +"</p>");
 $("#tooltip")
  .css("top",(e.pageY - xOffset) + "px")
  .css("left",(e.pageX + yOffset) + "px")
  .fadeIn("fast");  
},
function(){
 this.t = "";
 $("#tooltip").remove();
}); 
$("a[rel=tooltip-1]").mousemove(function(e){
 $("#tooltip-1")
  .css("top",(e.pageY - xOffset) + "px")
  .css("left",(e.pageX + yOffset) + "px");
});

Two Issues:

  1. Tooltip div (#tooltip-1) doesn't hide on mouseout.
  2. Since the tooltip div will always have the same ID as REL, I'd like to modify the above jQuery so that it takes target DIV ID automatically.

Thanks in advance for your help.

Share Improve this question edited Apr 8, 2010 at 9:32 eozzy asked Apr 8, 2010 at 9:04 eozzyeozzy 68.9k109 gold badges285 silver badges447 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3
this.t = this.attr("href");

this is a DOM node, not a jQuery wrapper: there is no attr() method.

(I also don't see why you're writing to an expando t property on the node. You don't seem to use it anywhere else.)

$("body").append("<p id='tooltip'>"+ this.t +"</p>");

Try not to make HTML strings from plain text. Any & or < characters in t and your HTML is messed up. (This is an easy way to let cross-site-scripting security holes in.) Use text() to set plain text content in an element instead.

(Although, I'm not sure why you're writing the # attribute value into the tooltip. Are you trying to read the HTML of the tooltip div and copy it into the p? Why not just use the tooltip div itself?)

Tooltip div (#tooltip-1) doesn't hide on mouseout.

It doesn't hide/show at all, your script isn't touching it anywhere.

Since the tooltip div will always have the same ID as REL

How about putting this information in the href fragment, rather than abusing rel? Then the link actually points to the place it's going to pop up.

eg. something like:

<a href="#footnote1" class="tooltip">?</a>

<div id="footnote1" style="background: yellow; width: 10em;">
    Hello!
</div>

$('.tooltip').each(function() {
    var tip= $(this.hash);
    var dx= -30, dy= -tip.height()-10;
    tip.css('position', 'absolute');
    tip.hide();

    function position(e) {
        tip.css('left', e.pageX+dx+'px').css('top', e.pageY+dy+'px');
    }
    $(this).mousemove(position);

    $(this).hover(function(e) {
        position(e);
        tip.show('fast');
    }, function(e) {
        tip.hide('fast');
    });
});

(Or just use one of the many already-existing jQuery tooltip plugins.)

Just to add to the accepted answer: there is a much better alternative to jQuery's tooltips called jQuery tools.

Check out the demo.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744941967a4602379.html

相关推荐

  • javascript - jQuery Tooltip positioning issues - Stack Overflow

    HTML:<a href="#" rel="tooltip-1">Open Tooltip<a><div id="tool

    1天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信