简体   繁体   English

如何使用jQuery UI Tooltip在打开时更改工具提示颜色?

[英]How to change tooltip color on open using jQuery UI Tooltip?

I'm trying to achieve the following, can't figure out how though. 我试图实现以下,无法弄清楚如何。 I have several tooltips on a page, but I want each one to look different (depending on severity for instance). 我在页面上有几个工具提示,但我希望每个工具提示看起来不同(例如,根据严重程度)。 Here is what I have so far: 这是我到目前为止:

<div id="container">
<a href="#" title="Tooltip 1" data-severity="warning">Tooltip 1 (Warning)</a>
<a href="#" title="Tooltip 2" data-severity="danger">Tooltip 2 (Danger)</a>
</div>

<script type="text/javascript">
// init tooltip
ArrowToolTip.init('container');
</script>

/*JAVASCRIPT*/
var ArrowToolTip = {
init: function (parentId) {
    jQuery('#' + parentId).tooltip(
    {
        content: function () {
            var element = jQuery(this);
            return element.attr('title');
        },
        create: function () {
            var element = jQuery(this);

            var severity = element.data('severity');

            if (!severity) {
                severity = 'default';
            }

            element.tooltip('option', 'tooltipClass', severity);
        },
        open: function(event, ui) {
            var element = jQuery(this);
            var severity = element.data('severity');
            ui.tooltip({
                tooltipClass: severity 
            });
            //ui.tooltip.tooltipClass(severity);
            console.log(event);
            console.log(ui);
        }
    });
}
}

Whatever I have in the create: works, but it's only called on page load, so that doesn't do me any good. 无论我在创建中有什么:有效,但它只在页面加载时调用,所以这对我没有任何好处。 So now I'm trying to move this to the open: event. 所以现在我想把它移到open: event。 But that doesn't seem to work at all. 但这似乎根本不起作用。 The jQuery(this) doesn't return the hovered element, it returns the container. jQuery(this)不返回悬停元素,它返回容器。 How can I get the correct element to select the data attribute? 如何获取正确的元素来选择数据属性? And how can I then change the CSS class? 那我怎么能改变CSS类呢? Does anyone know how this could be achieved? 有谁知道如何实现这一目标?

I didn't get your logic very well, but here's how I achieve different classes depending on data-severity . 我没有很好地理解你的逻辑,但这是我如何根据data-severity实现不同的类。 Instead of applying .tooltip() to all elements, iterate through them and set the tooltipClass as $(this).data('severity') . 而不是将.tooltip()应用于所有元素,迭代它们并将tooltipClass设置为$(this).data('severity') The content function allows to add a class too. 内容函数也允许添加类。

 $('a.tips').each(function () { var severity = $(this).data('severity'); $(this).tooltip({ content: function () { return '<em>' + $(this).attr('title') + '</em>'; }, tooltipClass: severity, show: "slideDown", open: function (event, ui) { ui.tooltip.hover(function () { $(this).fadeTo("slow", 0.5); }); } }); }); 
 .ui-tooltip { padding: 10px 20px; border-radius: 20px; font: bold 14px"Helvetica Neue", Sans-Serif; text-transform: uppercase; box-shadow: 0 0 7px black; } .ui-tooltip.warning { color: #E3E8F7; background: #D94AA4; } .ui-tooltip.danger { color: #212942; background: #CABA75; } a.tips { float:left; margin: 0 10px; text-decoration:none; font: Sans-Serif; } body { background-color:#eee; padding: 30px; } 
 <link href="//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.10.4/css/jquery.ui.tooltip.min.css" rel="stylesheet"/> <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script> <div id="container"> <a href="#" title="Tooltip 1" class="tips" data-severity="warning">(Warning)</a> <a href="#" title="Tooltip 2" class="tips" data-severity="danger">(Danger)</a> </div> 

i am not clear about your quection. 我不清楚你的排错。 But i hope this will help you sometimes.... 但我希望这有时会帮助你......

I am using tooltipClass name as "tooltipclass" in my jquery. 我在我的jquery中使用tooltipClass名称作为“tooltipclass”。

$(document).tooltip({effect: "blind", duration: 1000, tooltipClass: 'tooltipclass'});

And using that class name you can give any styling to the tool tip. 使用该类名称,您可以为工具提示提供任何样式。

.tooltipclass{
    font-size: 12px;
    border:1px solid #e74c3c;
}

so try using different tooltipClass names......... 所以尝试使用不同的tooltipClass名称.........

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

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