是否可以使用div作为Twitter的Popover的内容

kir*_*rby 147 popover twitter-bootstrap

我使用Twitter的引导的酥料饼这里.现在,当我在滚动的酥料饼的文本酥料饼的出现从只是文字<a>data-content属性.我想知道是否有任何东西放在<div>弹出窗口内.潜在地,我想在那里使用php和mysql,但如果我能得到div工作,我想我可以弄清楚剩下的.我尝试将数据内容设置为divID,但它没有用.

HTML:

<a class='danger' 
   data-placement='above' 
   rel='popover' 
   data-content='#PopupDiv' 
   href='#'>Click</a>
Run Code Online (Sandbox Code Playgroud)

jäv*_*ävi 297

首先,如果要在内容中使用HTML,则需要将HTML选项设置为true:

$('.danger').popover({ html : true});
Run Code Online (Sandbox Code Playgroud)

然后,您有两个选项来设置Popover的内容

  • 使用data-content属性.这是默认选项.
  • 使用返回HTML内容的自定义JS函数.

使用数据内容:您需要转义HTML内容,如下所示:

<a class='danger' data-placement='above' 
   data-content="&lt;div&gt;This is your div content&lt;/div&gt;" 
   title="Title" href='#'>Click</a>
Run Code Online (Sandbox Code Playgroud)

您可以手动转义HTML或使用函数.我不知道PHP,但在Rails中我们使用*html_safe*.

使用JS函数:如果这样做,您有几个选项.我认为最简单的方法是将div内容隐藏在任何你想要的位置,然后编写一个函数将其内容传递给popover.像这样的东西:

$(document).ready(function(){
  $('.danger').popover({ 
    html : true,
    content: function() {
      return $('#popover_content_wrapper').html();
    }
  });
});
Run Code Online (Sandbox Code Playgroud)

然后你的HTML看起来像这样:

<a class='danger' data-placement='above' title="Popover Title" href='#'>Click</a>
<div id="popover_content_wrapper" style="display: none">
  <div>This is your div content</div>
</div>
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你!

PS:我在使用popover而没有设置title属性时遇到了一些麻烦...所以,记得要始终设置标题.

  • 仅供参考,bootstrap有一个名为"hide"的类,它设置display:none (5认同)

ish*_*ood 42

基于jävi的答案,可以在没有ID或其他按钮属性的情况下完成此操作:

http://jsfiddle.net/isherwood/E5Ly5/

<button class="popper" data-toggle="popover">Pop me</button>
<div class="popper-content hide">My first popover content goes here.</div>

<button class="popper" data-toggle="popover">Pop me</button>
<div class="popper-content hide">My second popover content goes here.</div>

<button class="popper" data-toggle="popover">Pop me</button>
<div class="popper-content hide">My third popover content goes here.</div>

$('.popper').popover({
    container: 'body',
    html: true,
    content: function () {
        return $(this).next('.popper-content').html();
    }
});
Run Code Online (Sandbox Code Playgroud)


A B*_*ker 11

另一种替代方法,如果你想拥有弹出的外观和感觉.以下是方法.当然这是一个手动的东西,但很好用:)

HTML - 按钮

<button class="btn btn-info btn-small" style="margin-right:5px;" id="bg" data-placement='bottom' rel="tooltip" title="Background Image"><i class="icon-picture icon-white"></i></button>
Run Code Online (Sandbox Code Playgroud)

HTML - popover

<div class="bgform popover fade bottom in">
            <div class="arrow"></div>
             ..... your code here .......
</div>
Run Code Online (Sandbox Code Playgroud)

JS

$("#bg").click(function(){
        $('.bgform').slideToggle();
});
Run Code Online (Sandbox Code Playgroud)


Rom*_*n K 10

迟到了.建立其他解决方案.我需要一种方法将目标DIV作为变量传递.这就是我做的.

用于Popover源的HTML(添加了一个数据属性data-pop,它将保存目标DIV id /或类的值):

<div data-html="true" data-toggle="popover" data-pop="popper-content" class="popper">
Run Code Online (Sandbox Code Playgroud)

用于Popover内容的HTML(我正在使用bootstrap hide class):

<div id="popper-content" class="hide">Content goes here</div>
Run Code Online (Sandbox Code Playgroud)

脚本:

$('.popper').popover({
placement: popover_placement,
container: 'div.page-content',
html: true,
trigger: 'hover',
content: function () {
    var pop_dest = $(this).attr("data-pop");
    //console.log(plant);
    return $("#"+pop_dest).html();
}});
Run Code Online (Sandbox Code Playgroud)


Bir*_*one 8

除了其他回复.如果允许htmlin选项,则可以将jQuery对象传递给内容,并且它将附加到包含所有事件和绑定的popover内容.以下是源代码的逻辑:

  • 如果你传递一个函数,它将被调用来解包内容数据
  • 如果html不允许,内容数据将作为文本应用
  • 如果html允许且内容数据为字符串,则将其应用为html
  • 否则内容数据将附加到popover的内容容器中
$("#popover-button").popover({
    content: $("#popover-content"),
    html: true,
    title: "Popover title"
});
Run Code Online (Sandbox Code Playgroud)


shr*_*use 5

所有这些答案都忽略了一个非常重要的方面!

通过使用 .html 或innerHtml 或outerHtml,您实际上并未使用引用的元素。您正在使用元素的 html 的副本。这有一些严重的缺点。

  1. 您不能使用任何 id,因为这些 id 会重复。
  2. 如果每次显示弹出窗口时都加载内容,您将丢失所有用户输入。

您想要做的是将对象本身加载到弹出窗口中。

https://jsfiddle.net/shrewmouse/ex6tuzm2/4/

HTML:

<h1> Test </h1>

<div><button id="target">click me</button></div>

<!-- This will be the contents of our popover -->
<div class='_content' id='blah'>
<h1>Extra Stuff</h1>
<input type='number' placeholder='number'/>
</div>
Run Code Online (Sandbox Code Playgroud)

查询:

$(document).ready(function() {

    // We don't want to see the popover contents until the user clicks the target.
    // If you don't hide 'blah' first it will be visible outside of the popover.
    //
    $('#blah').hide();

    // Initialize our popover
    //
    $('#target').popover({
        content: $('#blah'), // set the content to be the 'blah' div
        placement: 'bottom',
        html: true
    });
    // The popover contents will not be set until the popover is shown.  Since we don't 
    // want to see the popover when the page loads, we will show it then hide it.
    //
    $('#target').popover('show');
    $('#target').popover('hide');

    // Now that the popover's content is the 'blah' dive we can make it visisble again.
    //
    $('#blah').show();


});
Run Code Online (Sandbox Code Playgroud)