禁用后如何重新启用jQuery工具提示= true?

时间:2022-04-13 09:43:40

Trying to temporarily disable the jQuery tooltip

尝试暂时禁用jQuery工具提示

$(document).tooltip( "option", "disabled", true );

When I try to re-enable them again, all of the title attributes are gone. I was trying to re-enable them using:

当我试图重新启用它们时,所有的标题属性都消失了。我试图重新启用它们,使用:

$(document).tooltip( "option", "disabled", false);

The title attributes is actually being removed completely when i first set it to disabled to true.

当我第一次将标题属性设置为禁用为true时,标题属性实际上被完全删除。

Also tried:

也试过:

$(document).tooltip("disable");
$(document).tooltip("enable");

It is doing the same thing...

它正在做同样的事情……

Update

更新

Found a solution to my own question with the help of Jasen and zgr024. See below.

在Jasen和zgr024的帮助下找到了我自己问题的答案。见下文。

3 个解决方案

#1


4  

This appears to be a bug with the jquery version so you need a work-around to insert the title attribute after disabling tooltips.

这似乎是jquery版本的一个错误,因此在禁用工具提示后,您需要一个解决方案来插入title属性。

Use a class name on the tooltip element you need re-enabled or use the [title] attribute selector.

在需要重新启用的工具提示元素上使用类名或使用[title]属性选择器。

<input type="text" class="tooltip-hack" title="tooltip text" />

$("#disable").on("click", function (e) {
    var tooltips = $("[title]");  // or $(".tooltip-hack")
    $(document).tooltip("disable");
    tooltips.attr("title", "");
});

Use the least destructive of the two depending on your html structure.

根据您的html结构,使用两者中破坏性最小的一个。

Also, you note that all title attributes are removed so be more selective with your tooltip.

另外,您注意到所有的标题属性都被删除了,所以您的工具提示要更有选择性。

// instead of
$(document).tooltip();

// use a more restrictive selector
$(".tooltip-hack").tooltip();

Working example: jsFiddle

工作的例子:jsFiddle

#2


4  

So with the help of others above, I finally found a better solution. That require no brutal fix to re-adding title to every single elements. Don't get me wrong, that fix will work, but performance is important (especially I still need to support IE8 ugh).

所以在别人的帮助下,我终于找到了一个更好的解决方案。这就不需要对每个元素重新添加标题进行残酷的修正。不要误解我的意思,这个修正是可行的,但是性能很重要(尤其是我仍然需要支持IE8 ugh)。

Basically I add a custom variable to the tooltip object, this can also be a global variable. Since everything is an Object in js, you can just add anything you want to it.

基本上,我向工具提示对象添加了一个自定义变量,它也可以是一个全局变量。因为所有东西都是js中的对象,所以你可以添加任何你想要的东西。

$(document).tooltip.temporarilyOff

Then when I initialize the jQuery tooltip, I just need to add a check in the open:

然后在初始化jQuery工具提示时,只需要在open中添加一个check:

var settings = {};
settings.tooltipClass = "tooltip";
settings.open = function (event, ui) {
    if ($(document).tooltip.temporarilyOff) {
        ui.tooltip.stop().remove();
    }
};
$(document).tooltip(settings);

Then when I need to temporarily disable the jQuery tooltip, I just need to toggle the flag anywhere I want. Like so:

然后,当我需要暂时禁用jQuery工具提示时,我只需要在任何我想要的地方切换标志。像这样:

$(document).tooltip.temporarilyOff = true;

Anything after this point, the tooltip won't be triggered, and all the elements will keep their title attributes. When I am done with what I am doing, I just need to set the flag back to false and tooltip will work exactly like before.

在此之后,工具提示将不会被触发,所有元素都将保留它们的标题属性。当我完成我正在做的事情时,我只需要将标志设置为false,工具提示将与以前一样工作。

I can probably make this into jQuery plugin for easier calls and also hide the slightly ugly variable name... but anyway that's the idea. I think this is a much better fix as it won't force jQuery to remove the title attribute for nothing, then adding it back afterward doing twice the useless work.

我可以把它放到jQuery插件中进行更简单的调用,也可以隐藏稍微难看的变量名……总之就是这样。我认为这是一个更好的修复,因为它不会迫使jQuery毫无代价地删除title属性,然后再添加它,然后再做两次无用的工作。

Here is the updated example forked from @Jasen's original jsFiddle:

以下是来自@Jasen最初的jsFiddle的更新的例子:

#3


0  

Codenamezero is a nice example of extending an object in jQuery/js but you can do this out-of-the-box:

codename0是用jQuery/js扩展对象的一个很好的例子,但是您可以开箱即用:

$(document).tooltip("disable").tooltip("hide"); 

$(document).tooltip("enable").tooltip("show");  

I have validation where this method can be useful to override defaults

我有一个验证,这个方法可以用来重写默认值

#1


4  

This appears to be a bug with the jquery version so you need a work-around to insert the title attribute after disabling tooltips.

这似乎是jquery版本的一个错误,因此在禁用工具提示后,您需要一个解决方案来插入title属性。

Use a class name on the tooltip element you need re-enabled or use the [title] attribute selector.

在需要重新启用的工具提示元素上使用类名或使用[title]属性选择器。

<input type="text" class="tooltip-hack" title="tooltip text" />

$("#disable").on("click", function (e) {
    var tooltips = $("[title]");  // or $(".tooltip-hack")
    $(document).tooltip("disable");
    tooltips.attr("title", "");
});

Use the least destructive of the two depending on your html structure.

根据您的html结构,使用两者中破坏性最小的一个。

Also, you note that all title attributes are removed so be more selective with your tooltip.

另外,您注意到所有的标题属性都被删除了,所以您的工具提示要更有选择性。

// instead of
$(document).tooltip();

// use a more restrictive selector
$(".tooltip-hack").tooltip();

Working example: jsFiddle

工作的例子:jsFiddle

#2


4  

So with the help of others above, I finally found a better solution. That require no brutal fix to re-adding title to every single elements. Don't get me wrong, that fix will work, but performance is important (especially I still need to support IE8 ugh).

所以在别人的帮助下,我终于找到了一个更好的解决方案。这就不需要对每个元素重新添加标题进行残酷的修正。不要误解我的意思,这个修正是可行的,但是性能很重要(尤其是我仍然需要支持IE8 ugh)。

Basically I add a custom variable to the tooltip object, this can also be a global variable. Since everything is an Object in js, you can just add anything you want to it.

基本上,我向工具提示对象添加了一个自定义变量,它也可以是一个全局变量。因为所有东西都是js中的对象,所以你可以添加任何你想要的东西。

$(document).tooltip.temporarilyOff

Then when I initialize the jQuery tooltip, I just need to add a check in the open:

然后在初始化jQuery工具提示时,只需要在open中添加一个check:

var settings = {};
settings.tooltipClass = "tooltip";
settings.open = function (event, ui) {
    if ($(document).tooltip.temporarilyOff) {
        ui.tooltip.stop().remove();
    }
};
$(document).tooltip(settings);

Then when I need to temporarily disable the jQuery tooltip, I just need to toggle the flag anywhere I want. Like so:

然后,当我需要暂时禁用jQuery工具提示时,我只需要在任何我想要的地方切换标志。像这样:

$(document).tooltip.temporarilyOff = true;

Anything after this point, the tooltip won't be triggered, and all the elements will keep their title attributes. When I am done with what I am doing, I just need to set the flag back to false and tooltip will work exactly like before.

在此之后,工具提示将不会被触发,所有元素都将保留它们的标题属性。当我完成我正在做的事情时,我只需要将标志设置为false,工具提示将与以前一样工作。

I can probably make this into jQuery plugin for easier calls and also hide the slightly ugly variable name... but anyway that's the idea. I think this is a much better fix as it won't force jQuery to remove the title attribute for nothing, then adding it back afterward doing twice the useless work.

我可以把它放到jQuery插件中进行更简单的调用,也可以隐藏稍微难看的变量名……总之就是这样。我认为这是一个更好的修复,因为它不会迫使jQuery毫无代价地删除title属性,然后再添加它,然后再做两次无用的工作。

Here is the updated example forked from @Jasen's original jsFiddle:

以下是来自@Jasen最初的jsFiddle的更新的例子:

#3


0  

Codenamezero is a nice example of extending an object in jQuery/js but you can do this out-of-the-box:

codename0是用jQuery/js扩展对象的一个很好的例子,但是您可以开箱即用:

$(document).tooltip("disable").tooltip("hide"); 

$(document).tooltip("enable").tooltip("show");  

I have validation where this method can be useful to override defaults

我有一个验证,这个方法可以用来重写默认值