javascript - Bootstrap 工具提示 - 使用 jquery 动态更改 css 和位置

标签 javascript jquery html twitter-bootstrap

我正在使用 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 .这只是我的第二个 fiddle ,我不知道如何添加 Bootstrap 来显示工具提示。

最佳答案

主要问题

您忘记将 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

关于javascript - Bootstrap 工具提示 - 使用 jquery 动态更改 css 和位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33967475/

相关文章:

javascript - PHP 中传入 http 请求的识别

javascript - 我如何在类里面使用 jquery slide toggle?

html - 在包装之前使 flexbox 缩小尺寸

javascript - 如果更改输入值,jQuery 提交表单

javascript - 如何使用 Javascript 或 jQuery 表单编辑 .txt 文件

jquery - 使用 jquery 更改 html 标签的背景

javascript - Javascript "Math.sin"和 WebGL“sin”之间的区别

Javascript DOM 操作,每次点击创建一个新的 li 元素

javascript - 如何将 vue.js 事件附加到现有 html 元素

javascript - 在多个 Browserify 或 Webpack 包中共享通过 NPM 加载的模块的简单解决方案