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

后端 未结 8 1357
一向
一向 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: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:

    Test

    Extra Stuff

    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();
    
    
    });
    

提交回复
热议问题