How to show an image in tooltip?

2019-08-17 11:57发布

问题:

I want to show an image in a tooltip when you have your mouse on a link. I'm using HTML, CSS and JAVASCRIPT/JQUERY. I have my image saved in a folder so I reference it from localhost.

I have tried to set the content of the tooltip vía JQuery:

$(document).ready(function() {
    $('#informe').tooltip("option","content","<img src='images/reports/informeInversiones.PNG' />");
    $('#informe').tooltip("option","html","true");
    $('#informe').tooltip("option","animated","fade");
    $('#informe').tooltip("option","placement","bottom");
});

And I have tried to set the content of the tooltip in pure HTML:

<td colspan="2" id="informe" data-toggle="tooltip" title="<img src='images/reports/informeInversiones.PNG'/>"><p><i class="fas fa-download"></i><a id="save"> Generar Informe</a></p></td>

An then, in JQuery:

$(document).ready(function() {
    $('#informe').tooltip("option","html","true");
    $('#informe').tooltip("option","animated","fade");
    $('#informe').tooltip("option","placement","bottom");
});

I have a link which on onclick calls a JS function that downloads a PDF. I want that when you put the mouse on the link, it shows a preview (a simple image) of the PDF you're about to generate. I have tried the solutions I've seen in other questions of this page but they don't work.

This is how I dispose the link:

<td colspan="2"><p><i class="fas fa-download"></i><a href="#" id="informe" title="">Generar Informe</a></p></td>

And this is the JQuery:

$('#informe').tooltip({content: "<img src='images/reports/informeInversiones.PNG'/>"});

I've tried even to put simple text in content: but it doesn't even show a tooltip.

回答1:

try this.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <script>
  $(document).ready(function() {
  	$("#content").html('<a id="magilla" href="#" title="" >Magilla Gorilla</a>');
   	$("#content #magilla").tooltip({ content: '<img src="https://i.etsystatic.com/18461744/r/il/8cc961/1660161853/il_794xN.1660161853_sohi.jpg" />' }); 

	});
  </script>
</head>
<body>

<div id="content">    
</div>

</body>
</html>



回答2:

Here is a sniped for a image tooltip on a link. I am sure u can use this code for your case

a.tooltip {outline:none; }
a.tooltip strong {line-height:30px;}
a.tooltip:hover {text-decoration:none;} 
a.tooltip span {
    z-index:10;display:none; padding:14px 20px;
    margin-top:60px; margin-left:-160px;
    width:300px; line-height:16px;
}
a.tooltip:hover span{
    display:inline; position:absolute; 
    border:2px solid #FFF;  color:#EEE;
    background:#333 url(http://www.menucool.com/tooltip/cssttp/css-tooltip-gradient-bg.png) repeat-x 0 0;
}

.callout {z-index:20;position:absolute;border:0;top:-14px;left:120px;}
    
/*CSS3 extras*/
a.tooltip span
{
    border-radius:2px;        
    box-shadow: 0px 0px 8px 4px #666;
    /*opacity: 0.8;*/
}
<a href="https://codepen.io/anon/pen/rgPKvy" class="tooltip">
    Link
    <span>
        <img class="callout" src="https://freefrontend.com/assets/img/css-tooltips/pure-css-tooltips.png" />
        <img src="https://freefrontend.com/assets/img/css-tooltips/pure-css-tooltips.png" style="float:right;" />
        <strong>CSS only Tooltip</strong><br />
        Pure CSS popup tooltips with clean semantic XHTML.
    </span>
</a>