关闭Body Click事件的工具提示-工作不正常
Close Tooltip on Body Click Event - not working properly
我有一把小提琴:http://jsfiddle.net/u8t77p75/
我想实现这一点,当我点击"body"时,js会检查是否有实际的提示可见,如果是,则隐藏它。
我想试试这样的东西:
if ($tooltipContainer.hasClass('active')) {
$('body').click(function() {
$tipLink.triggerHandler('click');
});
}
它有效,但并不完美。当我插入这个片段时,它会调用触发器,但我需要单击几次才能打开或关闭。我认为这是因为整个点击函数get都被调用了。我需要做其他事情吗?
您可以在文档中添加一个点击处理程序,用于检查您是否在工具提示之外点击了,以及是否有一个打开的。代码如下:
$(document).click(function(e){
if ($(e.target).not(':has(.lhde__tooltip)').length == 0 && $('div.opened').length) {
// current click target is not the tooltip and a tip is open
$('div.opened').remove();
}
});
您的代码可以工作,但我会从引导程序中查看popover,因为创建popover更容易。点击此处查看工作演示
请在下面和jsFiddle上找到您的代码。
(function ($) {
'use strict';
var $tipLink = $('.lhde__tooltip'),
$tipContent = $('.lhde__tooltip__content');
//initial hide tipContent
$tipContent.hide();
$tipLink.click(function (e) {
// prevent click event
e.preventDefault();
var $clicked = $(this),
href = $(this).attr('href'),
$tooltipContainer = $(href);
// if a container with the id was found
if ($tooltipContainer.length) {
// if the tooltip is not already active
if (!$tooltipContainer.hasClass('active')) {
$tipContent.removeClass('active');
$('.lhde__tooltip__content.opened').remove();
$tooltipContainer.addClass('active');
$clicked.append('<div class="lhde__tooltip__content opened">' + $tooltipContainer.html() + '</div>');
// hide the tooltip
} else {
$tipContent.removeClass('active');
$('.lhde__tooltip__content.opened').remove();
}
}
});
$(document).click(function(e){
if ($(e.target).not(':has(.lhde__tooltip)').length == 0 && $('div.opened').length) {
// current click target is not the tooltip and a tip is open
//console.log($('div.opened'));
$('div.opened').remove();
//$tipContent.removeClass('active');
}
});
})(jQuery);
.content {
width: 150px;
background: #eee;
color: #333;
margin: 50px auto;
}
a {
color: black;
text-decoration: none;
display: block;
margin: 20px 0;
position: relative;
padding: 20px;
text-align: center;
}
.lhde__tooltip__content {
position: absolute;
background: #333;
color: white;
padding: 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="content"> <a class="lhde__tooltip" href="#kardiologen">Kardiologen</a>
<a class="lhde__tooltip" href="#pneumologen">Pneumologen</a>
</div>
<div class="lhde__tooltip__content" id="kardiologen">
<h3 class="lhde__third-headline">Kardiologen</h3>
<p class="lhde__paragraph">Lorem ipsum dollor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum soccis natoque penatabus et magais dis partiruent.</p> <span class="lhde__icon lhde__icon--close"></span>
</div>
<div class="lhde__tooltip__content" id="pneumologen">
<h3 class="lhde__third-headline">Pneumologen</h3>
<p class="lhde__paragraph">Lorem ipsum dollor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum soccis natoque penatabus et magais dis partiruent.</p> <span class="lhde__icon lhde__icon--close"></span>
</div>
相关文章:
- Javascript.getHours()工作不正常
- removeClass函数没有'工作不正常
- jQuery Ajax GET请求工作不正常
- Cordova ng路线工作不正常
- 更新属性工作不正常Meteor/MongoDB
- 为什么文本对齐:对;工作不正常
- Javascript作用域和Ajax调用;工作不正常
- JavaScript DOM insertBefore'工作不正常
- Angular Js布线工作不正常
- onmouseout函数工作不正常
- 承诺工作不正常
- JQuery.live()工作不正常
- Alloy UI文本区域工作不正常
- ng src工作不正常,但src工作正常
- Canvas.toDataUrl(“image/png”)工作不正常
- JQuery `length`属性工作不正常.为什么?
- Javascript setInterval工作不正常
- 我的Javascript代码没有'工作不正常
- 表单验证工作不正常,在不检查条目的情况下继续
- $localStorage array select with ng选项工作不正常