【问题标题】:How to create Bootstrap Popover Close Option如何创建 Bootstrap Popover 关闭选项
【发布时间】:2013-03-14 11:01:24
【问题描述】:

正如您在jQuery 中看到的那样,我使用this question 的答案使 Bootstrap Popover 在外部单击时消失。现在我希望在右上角添加一个“x”,以便在单击时关闭弹出框。

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

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:

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

【问题讨论】:

  • @JorgeCode 的代码有效。我使用第一个而不在标题行中添加关闭按钮。 (由于缺乏特权,无法对 Jorge Code 的答案发表评论)

标签: javascript jquery html css twitter-bootstrap


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    这里是jquery代码:

    这个只是在右上角添加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
                  }
                });
    

    只有在单击 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();
                    });
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-24
    • 2021-02-12
    相关资源
    最近更新 更多