Is it possible to use a div as content for Twitter's Popover

后端 未结 8 1335
一向
一向 2020-11-28 01:00

I am using twitter\'s bootstrap\'s popover here. Right now, when i scroll over the popover text a popover appears with just text from the \'s da

相关标签:
8条回答
  • 2020-11-28 01:23

    First of all, if you want to use HTML inside the content you need to set the HTML option to true:

    $('.danger').popover({ html : true});
    

    Then you have two options to set the content for a Popover

    • Use the data-content attribute. This is the default option.
    • Use a custom JS function which returns the HTML content.

    Using data-content: You need to escape the HTML content, something like this:

    <a class='danger' data-placement='above' 
       data-content="&lt;div&gt;This is your div content&lt;/div&gt;" 
       title="Title" href='#'>Click</a>
    

    You can either escape the HTML manually or use a function. I don't know about PHP but in Rails we use *html_safe*.

    Using a JS function: If you do this, you have several options. The easiest I think is to put your div content hidden wherever you want and then write a function to pass its content to popover. Something like this:

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

    And then your HTML looks like this:

    <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>
    

    Hope it helps!

    PS: I've had some troubles when using popover and not setting the title attribute... so, remember to always set the title.

    0 讨论(0)
  • 2020-11-28 01:30

    In addition to other replies. If you allow html in options you can pass jQuery object to content, and it will be appended to popover's content with all events and bindings. Here is the logic from source code:

    • if you pass a function it will be called to unwrap content data
    • if html is not allowed content data will be applied as text
    • if html allowed and content data is string it will be applied as html
    • otherwise content data will be appended to popover's content container
    $("#popover-button").popover({
        content: $("#popover-content"),
        html: true,
        title: "Popover title"
    });
    
    0 讨论(0)
  • 2020-11-28 01:32

    Why so complicated? just put this :

    data-html='true'
    
    0 讨论(0)
  • 2020-11-28 01:35

    All of these answers miss a very important aspect!

    By using .html or innerHtml or outerHtml you are not actually using the referenced element. You are using a copy of the element's html. This has some serious draw backs.

    1. You can't use any ids because the ids will be duplicated.
    2. If you load the contents every time that the popover is shown you will lose all of the user's input.

    What you want to do is load the object itself into the popover.

    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>
    

    JQuery:

    $(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();
    
    
    });
    
    0 讨论(0)
  • 2020-11-28 01:38

    Late to the party. Building off the other solutions. I needed a way to pass the target DIV as a variable. Here is what I did.

    HTML for Popover source (added a data attribute data-pop that will hold value for destination DIV id/or class):

    <div data-html="true" data-toggle="popover" data-pop="popper-content" class="popper">
    

    HTML for Popover content (I am using bootstrap hide class):

    <div id="popper-content" class="hide">Content goes here</div>
    

    Script:

    $('.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();
    }});
    
    0 讨论(0)
  • 2020-11-28 01:40
    here is an another example
    
    <a   data-container = "body" data-toggle = "popover" data-placement = "left" 
        data-content = "&lt;img src='<?php echo baseImgUrl . $row1[2] ?>' width='250' height='100' &gt;&lt;div&gt;&lt;h3&gt; <?php echo $row1['1'] ?>&lt/h3&gt; &lt;p&gt; &lt;span&gt;<?php echo $countsss ?>videos &lt;/span&gt;
    &lt;span&gt;<?php echo $countsss1 ?> followers&lt;/span&gt;
    &lt;/p&gt;&lt;/div&gt;
    <?php echo $row1['4'] ?>   &lt;hr&gt;&lt;div&gt;
    &lt;span&gt; &lt;button type='button' class='btn btn-default pull-left green'&gt;Follow  &lt;/button&gt;  &lt;/span&gt; &lt;span&gt; &lt;button type='button' class='btn btn-default pull-left green'&gt; Go to channel page&lt;/button&gt;   &lt;/span&gt;&lt;span&gt; &lt;button type='button' class='btn btn-default pull-left green'&gt;Close  &lt;/button&gt;  &lt;/span&gt;
    
     &lt;/div&gt;">
    
    <?php echo $row1['1'] ?>
      </a>
    
    0 讨论(0)
提交回复
热议问题