【发布时间】:2014-03-12 22:24:11
【问题描述】:
好的,我有这段代码,我为 3 个不同的项目设置了 $tooltip,其中只有 $tooltip 使用 ajax 刷新。效果很好,但是在 ajax 中刷新后我必须运行
$('.tooltip').remove();
这是这个ajax调用后的问题,$tooltip2和$tooltip3丢失了,=(。我也试过这个:
$tooltip = $('.tooltip').remove();
但我显然错了。
这是我设置 $tooltip 的代码
var $tooltip = null;
var $tooltip2 = null;
var $tooltip3 = null;
function ttp() {
$tooltip = $('.marcleidnot[title]').tooltip({
delay:100,
slideInSpeed: 300,
slideOutSpeed: 300,
bounce: false,
/*bounce: false*/
relative: false, // <-- Adding this should sort you out
effect: 'slide',
direction: 'down',
/*slideInSpeed: 300,
slideOutSpeed: 200,*/
position: 'top center',
offset: [-15, 0]
});
}
$( document ).ready(function() {
$tooltip2 = $('.fav[title]').tooltip({
delay:50,
slideInSpeed: 200,
slideOutSpeed: 200,
/*bounce: false,*/
relative: false, // <-- Adding this should sort you out
effect: 'slide',
direction: 'down',
/*slideInSpeed: 300,
slideOutSpeed: 200,*/
position: 'top center',
offset: [10, -2]
});
$tooltip3 = $('.nofav[title]').tooltip({
delay:50,
slideInSpeed: 200,
slideOutSpeed: 200,
/*bounce: true,*/
relative: false, // <-- Adding this should sort you out
effect: 'slide',
direction: 'down',
/*slideInSpeed: 300,
slideOutSpeed: 200,*/
position: 'top center',
offset: [10, -2]
});
});
还有 Ajax 调用
function notifications() {
$.ajax(
{
type: "POST",
//data: dataparam,
url: "<?php echo $notifurl;?>",
success: function(msg)
{
if(msg !="")
{
$("#ajaxnotif").empty().html(msg);
//$('.tooltip').remove();
$tooltip = $('.tooltip').remove();
ttp();
//$("#suggestDiv").show();
}
else
{
$("#ajaxnotif").empty().html(msg);
$tooltip = $('.tooltip').remove();
ttp();
//$("#suggestDiv").hide();
}
}
});
}
更新
回答后我编辑并执行
function destroyTooltips($targets) {
$targets.removeData('tooltip').unbind().next('div.tooltip').remove();
}
var $destroyThis = $('.marcleidnot[title]');
function notifications() {
$.ajax(
{
type: "POST",
//data: dataparam,
url: "<?php echo $notifurl;?>",
success: function(msg)
{
if(msg !="")
{
$("#ajaxnotif").empty().html(msg);
//$('.tooltip').remove();
destroyTooltips($destroyThis);
ttp();
//$("#suggestDiv").show();
}
else
{
$("#ajaxnotif").empty().html(msg);
destroyTooltips($destroyThis);
ttp();
//$("#suggestDiv").hide();
}
}
});
}
现在的问题是,当进行 ajax 调用时,工具提示永远保持打开状态..
但现在我不会失去 $tooltip2 和 $tooltip3
我做错了什么。
更新2
在充满细节的 2nd Great Answer 之后,为什么不重新创建工具提示?
function notifications() {
$.ajax(
{
type: "POST",
//data: dataparam,
url: "<?php echo $notifurl;?>",
success: function(msg)
{
if(msg !="")
{
$("#ajaxnotif").empty().html(msg);
//$('.tooltip').remove();
destroyTooltip($tooltip);
// If you update the title of the tooltip, it will be init correctly.
$tooltip.attr('title', msg);
initTooltip($tooltip, {
delay : 100,
slideInSpeed : 300,
slideOutSpeed: 300,
bounce : false,
offset : [-15, 0]
});
//$("#suggestDiv").show();
}
else
{
$("#ajaxnotif").empty().html(msg);
destroyTooltip($tooltip);
// If you update the title of the tooltip, it will be init correctly.
$tooltip.attr('title', msg);
initTooltip($tooltip, {
delay : 100,
slideInSpeed : 300,
slideOutSpeed: 300,
bounce : false,
offset : [-15, 0]
});
//$("#suggestDiv").hide();
}
}
});
}
【问题讨论】:
-
通过这个
$('.tooltip').remove();删除所有带有工具提示类的元素,这会创建插件。插件本身没有销毁方法。不要使用工具提示来通知,尝试类似ned.im/noty -
我有,我已经尝试了 2 个没有结果的解决方案,标记解决方案是好的,但是我做一个语法错误,我是否必须替换 $target 来替换我在 ajax 中复制了他的函数的东西我运行 destroyTooltips($tooltip);一点运气都没有:(
标签: javascript jquery tooltip jquery-tools