He estado tratando de hacer javascript tooltip muy simple con jQuery pero he golpeado una pared de ladrillo. La idea es tener un pequeño elemento en línea (span
) dentro de un div
. El elemento span
contendrá una información sobre herramientas div
con un pequeño html (imagen y enlace). La información sobre herramientas se debe abrir cuando se hace clic en el elemento span
y se cierra cuando se hace clic fuera de ella o fuera de la información sobre herramientas.cómo cerrar jQuery tooltip
Hasta ahora, la apertura de la información sobre herramientas no es un problema, pero el cierre sí lo es.
<!DOCTYPE HTML>
<html>
<head>
<title></title>
<style>
#colors > div {
background-color: red;
height: 50px;
width: 50px;
margin: 5px;
}
#colors > div > span {
min-height: 10px !important;
min-width: 10px !important;
border: 3px solid black;
position: relative;
}
.tooltip {
border: 2px solid blue;
display: none;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
$(function() {
// generate boxes and tooltips
for (var i = 0; i < 9; i++) {
$('#colors').append('<div id="' + i + '"><span><div class="tooltip"><a href="#">add to favorites</a></div></span></div>');
}
$('#colors').delegate('span', 'click', function (event) {
$(this).children('.tooltip').css({position:'absolute', top:'5px', left:'5px'}).fadeIn();
// bottom one won't work
//event.stopPropagation();
});
$(document).delegate('body', 'click', function (event) {
var that = this
$.each($('.tooltip'), function (index, element) {
// it's always visible ...
//if ($(element).is(':visible')) {
// doesn't work either
if ($(element).is(':visible') && $(element).has(event.target).length === 0) {
var s = event.target;
console.log([($(s) == event.target), event.target, index, element, $(element).has(event.target).length, that]);
}
});
});
})
</script>
</head>
<body>
<div id="colors"></div>
</body>
</html>
Me parece que no puede encontrar una manera de cerrar la etiqueta de ayuda si es clic fuera de la span
y descripción.
favor, trate de añadir un poco más de información a sus respuestas. –