【问题标题】:bootstrap tooltip - use jquery to dynamically change css & placement引导工具提示 - 使用 jquery 动态更改 css 和放置
【发布时间】:2016-04-07 19:54:57
【问题描述】:

我正在使用 Bootstrap v3.3.5 和 JQuery v1.11.3。

I have a select list that when a value is selected, I would like the placement and css class of a tooltip to change.

我搜索了 Google & SO 并阅读了许多 SO 帖子,但我所做的许多尝试都失败了。例如,this 对我不起作用。

这是我的html代码:

<select id="id_cover_letter_type" name="cover_letter_type">
    <option value="0" selected="selected">I will write my own data</option>
    <option value="1">Standard Cover Letter</option>
    ....
</select>

<i id="id_icon_id_cover_letter_details_second_paragraph" class="fa fa-lightbulb-o blue_color icon_size26 verticalAlignTop" data-original-title="" title="" aria-describedby="tooltip229278"></i>

这是我的 jquery 代码:

我已将我为更改工具提示的数据放置(取自其他帖子)所做的已注释掉的尝试包括在内,但这些尝试对我不起作用。

$("#id_icon_id_cover_letter_details_second_paragraph").tooltip({'html': true, 'placement': 'bottom', 'title': '{% trans "Paragraph 2" %} <br /><br /> {% trans "This should detail why you want the job." %} <br /><br /> {% trans "Explain how your qualifications and career plan match the advertised position." %} <br /><br /> {% trans "The details you provide here should display that you have researched the employer, what the employer and industry are seeking and that you understand what the job requires." %}'});

if ( $('#id_cover_letter_type').val() == '0' ) {

    // the following code did not work.
    //$('#id_icon_id_cover_letter_details_second_paragraph').data('bs.tooltip').options.placement = 'left';

    // the following code did not work.
    //$('#id_icon_id_cover_letter_details_second_paragraph').tooltip({ placement: 'left' });

    // the following, destroying, appling the placement and then showing the tooltip did not work.
    //$('#id_icon_id_cover_letter_details_second_paragraph').tooltip('destroy');
    //$("#id_icon_id_cover_letter_details_second_paragraph").tooltip({'placement':'left'});
    //$("#id_icon_id_cover_letter_details_second_paragraph").tooltip('show');

    // applying the placement and the showing the tooltip did not work.
    //$("#id_icon_id_cover_letter_details_second_paragraph").tooltip({placement : 'left'}).tooltip('show');

} else {

    // the following code did not work.
    //$('#id_icon_id_cover_letter_details_second_paragraph').data('bs.tooltip').options.placement = 'bottom';

    // the following code did not work.
    //$('#id_icon_id_cover_letter_details_second_paragraph').tooltip({ placement: 'bottom' });

    // the following, destroying, appling the placement and then showing the tooltip did not work.
    //$('#id_icon_id_cover_letter_details_second_paragraph').tooltip('destroy');
    //$("#id_icon_id_cover_letter_details_second_paragraph").tooltip({'placement':'bottom'});
    //$("#id_icon_id_cover_letter_details_second_paragraph").tooltip('show');

    // applying the placement and the showing the tooltip did not work.
    //$("#id_icon_id_cover_letter_details_second_paragraph").tooltip({placement : 'bottom'}).tooltip('show');

}

另外,如果有人能告诉我将工具提示的 css 更改为类名custom-tooltip-styling123 的答案,那就太好了。

编辑

这是jsfiddle。这只是我的第二把小提琴,我不知道如何在引导程序中添加以显示工具提示。

【问题讨论】:

  • 那么当您选择一个选项时,工具提示类将被更改?
  • 你能做同样的jsfiddle吗
  • Jim Fahad,是的,工具提示类和工具提示数据放置会改变。
  • Ankur140290 - 我在帖子中添加了一个小提琴。
  • 您可以了解更多关于 -Tooltip 插件是当用户将鼠标指针移到元素上时出现的小弹出框:w3schools.com/bootstrap/bootstrap_tooltip.asp

标签: javascript jquery html twitter-bootstrap


【解决方案1】:

主要问题

您忘记将id="id_icon_id_cover_letter_details_second_paragraph" 添加到您的img 元素。

你还需要用下面的代码包装你的js代码:

$(function () { // On document ready wrapper
    // your code ...
});

最后一点是添加工具提示初始化:

$('#id_icon_id_cover_letter_details_second_paragraph').tooltip();

查看完整代码jsfiddle

如何更改工具提示的css

您可以在 stackoverflow 上找到几种解决方案,例如 this
tooltip 有选项 template,其默认值为:

'<div class="tooltip" role="tooltip"><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>'  

您可以添加CUSTOM_CSS并传递此选项的更新版本,例如:

'<div class="tooltip CUSTOM_CSS" role="tooltip"><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>'  

或者你可以为.tooltip类定义你自己的css规则。

其实还有更多的解决方案。这是你的选择,选择其中之一。您知道tooltip 元素的html 结构和类,因此可以随意操作它。

有关template选项要求的更多信息,请查看官方文档here

【讨论】:

  • 拉沙德,谢谢。我看了这么久,我再也看不到错误了。您知道如何更改工具提示的 css,以便我可以更改不同位置之间的宽度吗?
  • 哦,你编辑了你的评论。因此,要更改宽度,您只需在 css 规则中定义它。例如,.tooltip.right { width: 300px; } 将更改右侧位置的宽度,.tooltip.bottom { width: 300px; } 将更改底部位置的宽度,左侧和顶部位置的宽度相同。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-23
相关资源
最近更新 更多