0
ボタンをクリックすると、p
要素のツールチップを開き、ボタンを再度クリックするとツールチップを閉じます。動的に追加されたクラスのmouseleaveでjQueryのツールチップを閉じるのを防ぎます
$(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>
私が今やろうとすると、ボタンがクリックされたときに、私は動的に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>