如何更新实时在jQuery UI的工具提示的内容?(How do I Update the Cont

2019-08-19 08:34发布

我有一个在上空盘旋,它会显示物品的价格(在游戏中)的元素。 我使用jQuery UI提示供电的每个项目的信息的显示。 当单击该项目,jQuery的捕捉点击,使用$获得()请求处理购买,并且可以返回与通过JSON和jQuery的parseJSON功能项的具体信息。

然而,每个项目的价格与每次购买而改变。 这有点通货膨胀的一种形式。 我无法弄清楚如何访问jQuery UI的工具提示的内容,以改变它的内容,以便同时它仍然显示或不是显示,即使它,改变价值它的内容以反映新的价格。

我需要做什么做实时更改这些内容?

Answer 1:

您可以初始化后更改jQuery的工具提示的内容如下:

$( ".selector" ).tooltip( "option", "content", "Awesome title!" );

这里是一个演示 。

见API的更多细节。



Answer 2:

我只是碰到了这个自己,和其他的答案在这里真的不覆盖OP问(我有同样的问题)问题:如何让jQueryUI的工具提示修改改变“称号”的属性时,它的内容有一个提示的项目。 我有没有“title”属性,这是经由AJAX验证对服务器的输入,如果不是有效的,我是改变CSS类,添加一个“头衔”,以指示错误,并在jQuery用户界面的提示计数到“只是工作”。

我曾根据JQUI文档设置提示,使用$(document).tooltip()来获取默认,委托行为(其中显示了每个启用了项目的工具提示使用“title”属性)。 然后,当我的AJAX验证使用$("#inputid").attr("title", "validation error"); ,一切都是可爱的,我的任何错误信息出现的时候提示我输入的是徘徊。

然而,用户之后去除“标题”属性校正的输入值,通过调用$("#inputid").removeAttr("title"); 被证明是麻烦 - 因为“标题”属性会莫名其妙的重新出现,与工具提示一起。

一个小挖之后,我发现,这是因为工具提示部件(使用存储在另一个地方“标题”属性.data("ui-tooltip-content"...)禁用浏览器的实际工具提示,然后恢复它(这就是为什么“title”属性被神秘地重新出现。

所以,“正确的”(或者简单的)答案OP的问题是改变(或删除)“标题”属性之前暂时禁用jQueryUI的的提示功能,然后重新启用它后,把它重新审视内容和做正确的事。 因此,像这样:

// first, disable tooltips
$(document).tooltip("disable");

// Make your change to the element's title
$("#inputid").attr("title", "new message");
    or ...
$("#inputid").removeAttr("title");

// re-enable tooltips
$(document).tooltip("enable");

这里的所有都是它的! 使用的问题.tooltip("option", "content", "some text")如上,就是你需要手动定义要显示的每一个提示-肯定有很多不必要的编码,如果你想要的是有提示“只是工作”与页面内容。



Answer 3:

我的第一个答案是错误的,我们对此深感抱歉。

<div id='mybody'>
  <p title='Some tooltip'>paragraph</p>
</div>

JavaScript的:

function reloadToolTip(text){
  $('#mybody p').tooltip({contents:text});
}

你可以回忆一下提示,并更改内容。 这将改变提示但直到你rehover。

编辑:

我想我找到的东西,将工作。 只需关闭并重新打开提示。 该提示将包含新的文本。

function reloadToolTip(text){
  $('#mybody p').tooltip({contents:text}).tooltip('close').tooltip('open');
}


Answer 4:

下面是对我的作品还有另一种解决方案-只需添加这$(function(){ // code here });

$(document).tooltip({
    content: function()
    {
        var $this = $(this);
        return $this.attr('title');
    }
});

到这样做的好处是,如果你的title在运行时属性发生变化,工具提示始终显示最新版本,所以“更新”本身。

从技术上讲,我没有测试,如果这个工程与attr('title')我猜它会)。

我个人的版本是不同的,我需要的不仅仅是纯文本的工具提示中显示,即HTML代码。 因此,这里是我用我的当前项目的代码:

<div class="title" data-note="This is my tooltip text<br>with line break"></div>

$(document).tooltip({
    items: ".title",
    content: function()
    {
        var $this = $(this);
        if ($this.data('note') != '')
        {
            return unescape($this.data('note'));
        }
        else
        {
            return false;
        }
    }
});

需要注意items ,让我定制的元素来触发提示。



Answer 5:

为什么不这样说?

HTML:

 <input class="input-change" id="input1" type="text" title="Test1" value="Input #1">
 <input class="input-change" id="input2" type="text" title="Test2" value="Input #2">
 <input class="input-change" id="input3" type="text" title="Test3" value="Input #3">

jQuery的:

// Every time a key is pressed (for the sake of example)
$(function(){
    // Initialize tooltip
    $('.input-change').tooltip();

    // Every time a key is pressed (again, for the sake of example)
    $(".input-change").keyup(function(){
        // Get the actual value of the input that has changed (again, for the sake of example)
        var inputValue = $(this).val();

        // Get the id of the input that has changed
        var idInputChanged = "#" + $(this).attr('aria-describedby');

        // Go directly to the <div> inside the tooltip and change the text
        $(idInputChanged + ' div').html('Text changed in real time without closing tooltip!:<br />' + inputValue);
    });
})

的jsfiddle实施例:

http://jsfiddle.net/4MT5R/



Answer 6:

我使用的情况下参与制定了点击提示元素时成功/失败消息(复制网址到剪贴板),然后将其复位,当元素失去了悬停,所以当该元件,徘徊在旁边将显示原始邮件。

我注意到,当双击,即点击而提示仍处于打开状态,新的消息持续在元素丢失和恢复悬停。 这是我如何解决它。 我欢迎任何改进的建议。

var msg = 'New message';

// Cache $target
var $target = $(event.target);

// First close tooltip, which should be open on hover
$target.tooltip('close');

// Get the original message
var oldMsg = $target.tooltip("option", "content");

// Set the tool tip to the success/fail message
$target.tooltip("option", "content", msg);

// Open the tooltip with the new message
$target.tooltip('open');

// For some reason, the tooltip doesn't close automatically
// unless the hide option is reset
$target.tooltip("option", "hide", {effect: "fade", duration: 1000});

// Set message back to original after close.
$target.on("tooltipclose", function (event, ui) {
    $(this).tooltip("option", "content", oldMsg);
});


文章来源: How do I Update the Content in Jquery UI Tooltip in Realtime?