简体   繁体   English

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

[英]How to re-enable jQuery tooltip after disabled = true?

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. 当我尝试再次重新启用它们时,所有title属性都消失了。 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 . 当我第一次将其设置为disabledtrue时, title属性实际上被完全删除。

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. 在Jasen和zgr024的帮助下找到了我自己的问题的解决方案。 See below . 下文

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

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. 这不需要残酷的修复来为每个元素重新添加title Don't get me wrong, that fix will work, but performance is important (especially I still need to support IE8 ugh). 不要误会我的意思,修复会起作用,但性能很重要(特别是我仍然需要支持IE8)。

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中的所有内容都是Object ,因此您只需添加任何内容即可。

$(document).tooltip.temporarilyOff

Then when I initialize the jQuery tooltip, I just need to add a check in the open : 然后当我初始化jQuery工具提示时,我只需要在open添加一个检查:

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. 然后,当我需要暂时禁用jQuery工具提示时,我只需要在任何我想要的地方切换标志。 Like so: 像这样:

$(document).tooltip.temporarilyOff = true;

Anything after this point, the tooltip won't be triggered, and all the elements will keep their title attributes. 在此之后的任何事情,工具提示都不会被触发,并且所有元素都将保留其title属性。 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. 我可以把它变成jQuery插件,以便更容易调用,也可以隐藏稍微丑陋的变量名......但无论如何这就是主意。 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删除title属性,然后再将它添加回来做两次无用的工作。

Here is the updated example forked from @Jasen's original jsFiddle: 这是来自@Jasen的原始jsFiddle的更新示例

Codenamezero is a nice example of extending an object in jQuery/js but you can do this out-of-the-box: Codenamezero是在jQuery / js中扩展对象的一个​​很好的例子,但你可以开箱即用:

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

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

I have validation where this method can be useful to override defaults 我有验证这个方法可以用来覆盖默认值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM