Dynamically add a class to Bootstrap's 'popover' container

后端 未结 22 2439
名媛妹妹
名媛妹妹 2020-11-29 18:27

I\'ve thoroughly searched through both StackOverflow and Google, but come up empty. So apologies in advance if this has been asked & resolved already.

NB

相关标签:
22条回答
  • 2020-11-29 18:47

    I had the same problem, I liked the answer of @Kate, but changes in the source file can generate so much problems in the future, you probably will forget these little changes when you update your bootstrap's version. So I found another way of doing that:

     $(element).popover({...}).data("popover").tip().addClass("your_class")
    

    As @CorayThan fix it with data("popover")

    The method tip() of popover returns the popover element, and creates when it is not created, therefore you will always get the correct popover element, even if you are at the initialization of popover (This is my case =D ).

    0 讨论(0)
  • 2020-11-29 18:48

    You could use the container option to get around this.

    $('[data-toggle="popover"]').popover({
        container: '#foo'
    });
    

    or set it through a tag attribute

    <a href="#" data-toggle="popover" data-container="#foo" data-content="Content">Foo</a>
    

    And add this somewhere before closing the body tag

    <div id="foo"></div>
    

    Then you could do something like #foo > .popover. I know this is not a one-size-fits-all solution but it's one way to do it.

    0 讨论(0)
  • 2020-11-29 18:51

    Based on what @bchhun wrote and a lot of head scratching, I felt I should answer my own question, as I got it working. I also noticed this had been favourited and liked, so I hope I'm helping someone else who is a newbie at jQuery like myself.

    In the current Bootstrap build [v2.1.0], the scripts are all consolidated. So if you have included all of the scripts in your build (and not edited any new lines/taken some out), then head to line 1108 of the un-minified .js file. You'll find the following bit of code:

    $tip
      .css(tp)
      .addClass(placement)
      .addClass('in')
    

    You're going to be adding a new line to this, which is:

      .addClass(this.$element.attr("data-class"))
    

    So now whenever you add data-class to the popover call, it will add the attribute to the <div class="popover"> div.

    Now that I see it, it's so obvious :)

    0 讨论(0)
  • 2020-11-29 18:55

    best option that works on every vesion of BS, is to make it inside a specefic class and after showing that, find that class, and add your classname to popover parrent of it

    // create a template that add the message inside
    var $tmp = $("<div class='popoperrormessage'>" + error + "</div>");
    
    
                $(this).popover("destroy").popover({
                    trigger: "manual",
                    animation: false,
                    html: true,
                    placement:"right",
                    content: $tmp,
                    container: "body"
                }).popover("show");
    
    
                // now we have to find the parent of the popoperrormessagemessage
                $(".popoperrormessage").parents(".popover").addClass("hello");
    

    Now your popover will have hello class

    0 讨论(0)
  • 2020-11-29 18:56

    This works for me. It is inspired by the bootstrap's documentation from here, the Events section.

    $("a[rel=popover]").popover().on("show.bs.popover", function(){
        $(".popover").addClass("your-custom-class");
    });
    
    0 讨论(0)
  • 2020-11-29 18:57

    for bootstrap v3.3.2 you find these lines on bootstrap.js

       $tip
        .detach()
        .css({ top: 0, left: 0, display: 'block' })
        .addClass(placement)
        .data('bs.' + this.type, this)
    

    Then you add this line

        .addClass(this.$element.attr("data-class")) 
    

    Now to add a class on your popover element, you will be just putting an attribute data-class="classexemple" then everything works perfect

    Find us on www.mixpres.com

    0 讨论(0)
提交回复
热议问题