twitter-bootstrap 如何创建 Bootstrap Popover 关闭选项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15606760/
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 16:43:12  来源:igfitidea点击:

How to create Bootstrap Popover Close Option

javascriptjqueryhtmlcsstwitter-bootstrap

提问by Raphael Rafatpanah

As you can see in the jQuery, I have used the answer from this questionto make a Bootstrap Popover disappear on an outside click. Now I am looking to add an "x" in the top right corner that closes the popover on click.

正如您在jQuery 中看到的那样,我使用了这个问题的答案来使 Bootstrap Popover 在外部点击时消失。现在我想在右上角添加一个“x”,以在单击时关闭弹出窗口。

Is there a simple way to create a clickable "x" on the top right corner of the popover that would close the popover when clicked?

有没有一种简单的方法可以在弹出窗口的右上角创建一个可点击的“x”,点击时会关闭弹出窗口?

HTML:

HTML:

<h3>Live demo</h3>
<div class="bs-docs-example" style="padding-bottom: 24px;">
    <a href="#" class="btn btn-large btn-danger" data-toggle="popover" title="A Title" data-content="And here's some amazing content. It's very engaging. right?">Click to toggle popover</a>
</div>

jQuery:

jQuery:

var isVisible = false;
var clickedAway = false;

$('.btn-danger').popover({
    html: true,
    trigger: 'manual'
}).click(function(e) {
    $(this).popover('show');
    clickedAway = false
    isVisible = true
    e.preventDefault()
});

$(document).click(function(e) {
    if (isVisible & clickedAway) {
        $('.btn-danger').popover('hide')
        isVisible = clickedAway = false
    } else {
        clickedAway = true
    }
});

回答by CodeArtist

here is the jquery code:

这是jquery代码:

This one just adds the X button to the right upper corner:

这个只是在右上角添加了 X 按钮:

            var isVisible = false;
            var clickedAway = false;

            $('.btn-danger').popover({
                    html: true,
                    trigger: 'manual'
                }).click(function(e) {
                    $(this).popover('show');
                    $('.popover-title').append('<button type="button" class="close">&times;</button>');
                    clickedAway = false
                    isVisible = true
                    e.preventDefault()
                });

            $(document).click(function(e) {
              if(isVisible & clickedAway)
              {
                $('.btn-danger').popover('hide')
                isVisible = clickedAway = false
              }
              else
              {
                clickedAway = true
              }
            });

And this one closes the popup only when the X button is clicked:

并且只有在单击 X 按钮时才会关闭弹出窗口:

            $('.btn-danger').popover({
                    html: true,
                    trigger: 'manual'
                }).click(function(e) {
                    $(this).popover('show');
                    $('.popover-title').append('<button type="button" class="close">&times;</button>');
                    $('.close').click(function(e){
                        $('.btn-danger').popover('hide');
                    });
                    e.preventDefault();
                });

回答by Youngnate DaGreat

I kno this question has been answered already but your post semi-helped me . Stumbled across a simplier way to acomplish this task suppose you have all popovers with the class '.pop', this should solve all your problems

我知道这个问题已经得到回答,但你的帖子对我有帮助。偶然发现了一种更简单的方法来完成这个任务,假设你有所有带有“.pop”类的弹出框,这应该可以解决你所有的问题

$('.pop').on({
  click:function(){
    $('.pop').not(this).popover('hide');
  }
});