我彻底搜索了StackOverflow和谷歌,但一无所获.因此,如果这个问题已经被询问和解决了,请提前道歉.

我是jQuery的新手,所以我不知道自己该怎么写.我相信这是一段简单的代码片段,但我无法理解它.

What I'm looking to do is use a data- element (eg: data-class or similar) to attach a new class (Or ID, I'm not picky anymore!) to the top-level popover <div>. The code I currently have is as follows:

jQuery:

$('a[rel=popover]')
    .popover({
        placement : 'bottom',
        trigger : 'hover'
    })
    .click(function(e) {
        e.preventDefault()
    });

HTML:

<a href="" rel="popover" data-class="dynamic-class" title="Title goes here" data-content="Content goes here">

And ideally the kind of HTML I would have spit out, is something like this:

<div class="popover ... dynamic-class">
    <!-- remainder of the popover code as per usual -->
</div>

Is this something I can do? The documentation on the bootstrap site for popovers is a bit sparse, so it's taken me a while just to get to this point, unfortunately :(

Thanks in advance for any & all responses!

推荐答案

You can do this without hacking Bootstrap and without changing the template either, by grabbing the popover object from the caller's data and accessing its $tip property.

$('a[rel=popover]')
  .popover({ placement: 'bottom', trigger: 'hover' })
  .data('bs.popover')
  .tip()
  .addClass('my-super-popover');

Jquery相关问答推荐

jQuery中的多个 Select 器链接?

确定元素是否是其父元素的最后一个子元素

自动完成时触发的任何事件?

何时执行 $(document).ready 回调?

如何 Select 具有特定文本的所有锚标记

jquery 淡入淡出元素不显示样式为可见性:隐藏的元素

JavaScript 或 jQuery 字符串以实用函数结尾

jQuery 滚动到 Div

按百分比zoom div的内容?

如何使用jQuery动态设置宽度和高度

调用 e.preventDefault() 后提交表单

将数据传递给 jQuery UI 对话框

如何使用 jquery 更改 onclick 事件?

如何使所有浏览器都支持 ?有什么 Select 吗?

Jquery 和 HTML FormData 返回未捕获的 TypeError:非法调用

从附加元素获取 jQuery 对象的更简单方法

jQuery:value.attr 不是函数

你如何记录jQuery中一个元素触发的所有事件?

将十六进制转换为 RGBA

Bootstrap:在 Modal 中打开另一个 Modal