简体   繁体   English

如何为样式内容定义JQuery UI工具提示项选项

[英]How to define JQuery UI tooltip items option for styling content

I'm trying to create a tooltip for a few radio button options on a page. 我正在尝试为页面上的几个单选按钮选项创建工具提示。 I've got the tooltips displaying the [title] attribute easily enough, but I want to selectively format the tooltip text in a couple elements. 我已经足够轻松地显示了[title]属性的工具提示,但我想在几个元素中有选择地格式化工具提示文本。 I know I can provide content as HTML, and I've created some classes to style the content, but now the tooltips are no longer showing. 我知道我可以提供HTML内容,我已经创建了一些类来设置内容的样式,但现在工具提示不再显示了。 I think my problem is that I am not specifying the "items" option properly. 我认为我的问题是我没有正确指定“项目”选项。 In fact, I'm not quite sure how to define my html "content" option as an item at all. 事实上,我不太确定如何将html“content”选项定义为项目。 Halp plays! Halp播放!

JS JS

$('#tooltip').tooltip({
    tooltipClass: "tooltip-class"
    items: what do?
    content: "<div class="tooltip-header">header</div><div class="tooltip-description">text</div>"
});

HTML selected by JS 由JS选择的HTML

<a href="#" id="tooltip" class="link-info"></a>

Thanks for reading. 谢谢阅读。 :) :)

UPDATE: 更新:

Ok! 好! I figured it out... kinda. 我想通了......有点儿。 I was encasing my strings improperly: "<>"bad code"<>" . 我不恰当地包裹我的字符串:“<>”坏代码“<>”。 The solution to my problems with the content option was to put my html inside a variable and set it to content. 我对内容选项的问题的解决方案是将我的html放在变量中并将其设置为内容。 JQuery seems to have liked that much better. JQuery似乎更喜欢它。 My styles are showing up properly for the tooltip, but for some reason it is only working on one of three items selected for tooltips. 我的样式正好显示在工具提示中,但由于某种原因,它只针对为工具提示选择的三个项目之一。 There's nothing in the console that indicates it shouldn't work. 控制台中没有任何内容表明它不起作用。 No syntax errors, I'm selecting the correct id. 没有语法错误,我正在选择正确的ID。 I'm so confused. 我很混乱。

I think I know what you are running into. 我想我知道你遇到了什么。 When you use the content option you still have to include the title attribute on the element OR specify the items option as something different. 当您使用content选项时,您仍然必须在元素上包含title属性或将items选项指定为不同的选项。

I have NO IDEA why the jQuery team decided to do this, as it seems the selector you are applying the tooltip to, would be the same thing used for the items option. 我没有IDEA为什么jQuery团队决定这样做,因为看起来你正在应用工具提示的selector ,将与items选项使用相同的东西。 Quite duplicative. 相当重复。

Anyway, you can change the content but make sure the element you are applying a tooltip to has the title attribute or you specify the items option with a selector which could simply match the selector you are applying the tooltip to. 无论如何,您可以更改内容但确保要应用工具提示的元素具有title属性,或者使用选择器指定items选项,该选项器可以简单地匹配您应用工具提示的选择器。 Odd indeed. 奇怪的是。

Example: $('#selector').tooltip({ content: 'your content', items: '#selector' }); 示例: $('#selector').tooltip({ content: 'your content', items: '#selector' });

when you have problems with quotes, you have many options, like you said: 当你有引号问题时,你有很多选择,就像你说的那样:

  1. use another variable as you finally did 最后使用另一个变量
  2. escape the "inner" qoutes with backslashes like this: 用这样的反斜杠逃避“内部”qoutes:

     $('#tooltip').tooltip({ tooltipClass: "tooltip-class", items: "what do?", content: "<div class=\\"tooltip-header\\">header</div><div class=\\"tooltip-description\\">text</div>" }); 
  3. combine single and double quotes: 结合单引号和双引号:

     $('#tooltip').tooltip({ tooltipClass: "tooltip-class", items: "what do?", content: '<div class="tooltip-header">header</div><div class="tooltip-description">text</div>' }); 

you second problem after UPDATE : if you have created your link with id and you are applying tooltip with id selector 更新后的第二个问题:如果你用id创建了你的链接,你正在使用id选择器应用工具提示

<a href="#" id="tooltip" class="link-info"></a>

there can be only one link with same id in one html page! 在一个html页面中只能有一个具有相同id链接! so if you declared more links with the same id , most likely it wont work. 因此,如果您声明具有相同id更多链接,则很可能无法正常工作。

content: "<div class="tooltip-header">header</div><div class="tooltip-description">text</div>"

This produces a syntax error. 这会产生语法错误。

Please do the following: 请执行以下操作:

  • learn how to use your browser's JS error console 学习如何使用浏览器的JS错误控制台
  • learn the absolute basics of the JS syntax, and how to operate with strings/text literals 学习JS语法的绝对基础知识,以及如何使用字符串/文本文字进行操作

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

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