twitter-bootstrap Bootstrap - 以编程方式将弹出窗口附加并显示到元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28363716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 22:17:44  来源:igfitidea点击:

Bootstrap - Programmatically attach and show popover to an element

jquerytwitter-bootstraptwitter-bootstrap-3bootstrap-popover

提问by iltdev

I'm using Bootstrap v3 and I'm trying to show a popover programmatically, next to an element, when the page loads. There is no popover markup in the DOM. I want to create and show it in JQuery.

我正在使用 Bootstrap v3 并且我试图在页面加载时以编程方式在元素旁边显示一个弹出窗口。DOM 中没有弹出标记。我想在 JQuery 中创建并显示它。

I've tried this but it doesn't work.

我试过这个,但它不起作用。

$( document ).ready(function() {
   $('.theElement').popover({
            placement:'right',
            trigger:'manual',
            html:true,
            content:'popover content'
        });
   $('.theElement').popover('show')
});

I get a "TypeError: Argument 1 of Window.getComputedStyle is not an object" error in console. I'm assuming this is caused by the above.

我在控制台中收到“TypeError: Argument 1 of Window.getComputedStyle is not an object”错误。我假设这是由上述原因引起的。

回答by Dey-Dey

Try this

试试这个

$( document ).ready(function() {
   $('.theElement').attr('data-placement','right')
   //add all atributes.....

   //and finally show the popover
   $('.theElement').popover('show')
});

回答by Burhan Ibrahimi

    $("[wf-popover]").popover({
        html : true,
        content: function() {
          var content = $(this).attr("wf-content");
          return content;
        },
        title: function() {
          var title = $(this).attr("wf-title");
          //return $(title).children(".popover-heading").html();
          return title;
        },
        placement: function() {
            var my = this.$element.attr('wf-popover')
            return my;
        },
        trigger: 'hover'
    });

element

元素

<img src="profile.jpg" wf-popover="left" wf-content="<img src='profile_big.jpg' width=500 height=500 />" data-original-title="" title="">