简体   繁体   English

动态更改jQuery工具提示

[英]dynamically change jquery tooltip

jsfiddle example of what i am trying 我正在尝试的jsfiddle示例

  <div class="name1" title=" Air jordan ">mike jordan</div>

thats my html . 多数民众赞成在我的html。

I want to change the tooltip dynamically to say "air jordan 12:44pm is mike jordan" i know how to get the 12:44pm. 我想动态更改工具提示,说“ air jordan 12:44 pm是Mike jordan”,我知道如何获得12:44 pm。 what i dont know is how to change the tooltip dynamically while reading the original element. 我不知道的是如何在读取原始元素时动态更改工具提示。 I have to change the title element in order to change the tooltip right? 我必须更改标题元素才能更改工具提示对吗?

here is what I am trying 这是我正在尝试的

 $( ".name1" ).tooltip({
content: function() {
  console.log("1234");
  var element = $( this );
  return "gerd";
}
});

I feel like i am going down a very wrong path. 我觉得我走错了路。 I dont even see the console logs inside of the function. 我什至看不到该功能内部的控制台日志。 Is there a way to fix this? 有没有办法解决这个问题?

Or Should I 1. see if the element has a mouseenter, 2. then quickly change the title attritubte then 3 add a .tooltip? 还是我1.应该查看元素是否具有mouseenter,然后2.快速更改标题属性,然后3添加.tooltip?

Assuming that by 'tooltip' you mean the text that you see when you hover over the element (which is just the value of the title attribute), then you can use jQuery to simply edit the title attribute directly. 假设“工具提示”是指您将鼠标悬停在元素上时看到的文本(这只是title属性的值),那么您可以使用jQuery直接编辑title属性。

$('.name1').attr('title','air jordan ' + yourFormattedDateTimeFunction() + ' is mike jordan');

EDIT: 编辑:

A little Googling leads me to think you're referring specifically to the jQueryUI tooltip method, which is a bit fancier than standard tooltips. 稍加使用Google搜索就可以认为您是在专门指代jQueryUI工具提示方法,该方法比标准工具提示更新颖。 I'm not overly familiar with how this method works, but based on the API , I'd say your biggest problem is that you're passing a function handler to 'content', as opposed to actually executing a function which would create the string you need. 我对这种方法的工作方式不是很熟悉,但是基于API ,我想说的最大的问题是,您正在将函数处理程序传递给“内容”,而不是实际执行创建函数的函数。您需要的字符串。 I suggest you read up on Immediately Invoked Function Expressions for an explanation on the difference between these formats. 我建议您阅读“ 立即调用的函数表达式”以了解这些格式之间的区别。 So theoretically, your code should look something like this: 因此,从理论上讲,您的代码应如下所示:

$('.name1').tooltip({
    content: (function () {
                 console.log('this should show up now');
                 return 'air jordan ' + yourFormattedDateTimeFunction() + ' is mike jordan';
             })()
});

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

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