在下文中,单击按钮后,我会打开p
元素的工具提示,如果再次单击该按钮,则关闭工具提示。
$(document).ready(function() {
var obj = null;
$('input').click(function() {
if (obj == null) {
obj = $('p');
obj.tooltip({
items: 'p',
content: 'Some help'
});
obj.tooltip("open");
} else {
obj.tooltip('disable');
obj = null;
}
});
$('.help').mouseenter(function(e) {
e.stopImmediatePropagation();
});
$('.help').mouseleave(function(e) {
e.stopImmediatePropagation();
});
});
<link href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
<input type='button' value='Click me' />
<p class='help'>
Some text
</p>
我现在尝试做的是同样的事情,除了我在点击按钮时动态地将类'help'添加到p
元素。而不是工具提示保持打开状态,它会在鼠标悬停时消失。我发现mouseenter()/mouseleave()
处理程序没有触发,所以我用on()
替换它们。事件现在触发,但工具提示仍然关闭。
$(document).ready(function() {
var obj = null;
$('input').click(function() {
if (obj == null) {
obj = $('p');
obj.tooltip({
items: 'p',
content: 'Some help'
});
obj.tooltip("open");
obj.addClass('help'); // Added statically in DOM in above snippet
} else {
obj.tooltip('disable');
obj = null;
}
});
$(document.body).on('mouseenter', '.help', function(e) {
//console.log('on mouseenter');
e.stopImmediatePropagation();
});
$(document.body).on('mouseleave', '.help', function(e) {
//console.log('on mouseleave');
e.stopImmediatePropagation();
});
});
<link href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
<input type = 'button' value = 'Click me' />
<p>
Some text
</p>
答案 0 :(得分:1)
mouseleave
事件在远离Some text
后触发后,工具提示似乎被隐藏了。除非您需要使用mouseleave功能,否则您可以从工具提示中完全禁用此事件以获得所需的结果。
obj.tooltip("open");
obj.toggleClass("help"); // Toggle help class
obj.off("mouseleave"); // Remove the event from the tooltip
jsFiddle:https://jsfiddle.net/c1f0ft8j/