jQuery로 아주 간단한 javascript 툴팁을 만들려고 노력했지만 벽돌 벽을 맞았습니다. 작은 인라인 요소 (span
)를 div
안에 넣는 것이 좋습니다. span
요소에는 약간의 html (이미지 및 링크)이 포함 된 툴팁 div
이 포함됩니다. 도구 설명은 span
요소를 클릭 할 때 열어야하고 툴팁 외부 또는 툴팁 외부를 클릭 할 때 닫아야합니다.jQuery 툴팁을 닫는 방법
지금까지 툴팁을 여는 것은 문제가되지 않지만 닫는 것이 좋습니다.
<!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>
나는 클릭이 span
및 툴팁의 외부에있는 경우 도구 설명을 종료 할 수있는 방법을 찾을 수 없습니다.
답변에 좀 더 정보를 추가 해보세요. –