简体   繁体   English

如何用弹出窗口内的按钮关闭bootstrap-popover?

[英]How can i close a bootstrap-popover with a button inside the popover?

I have made a jsFiddle where i use twitter bootstrap popover function on an icon. 我已经制作了一个jsFiddle ,我在图标上使用了twitter bootstrap popover函数。

<div style="margin-top:200px">
    <ul>
        <li class="in-row">
            <a href="#" id="meddelanden" data-title="Meddelanden" data-toggle="clickover" 
            data-placement="right"><i class="icon-globe"></i></a>
        </li>
    </ul>
</div>

jquery: jQuery的:

var elem = '<div class="well"><a href="google.com">Message one, From someone.</a></div>'+
    '<div class="well"><a href="google.com">Message one, From someone.</a></div>'+
    '<button id="close-popover" class="btn btn-small btn-primary pull-right">Close please!</button>';

$('#meddelanden').popover({animation:true, content:elem, html:true});

I do not seem to be able to close the popover with the button inside it. 我似乎无法用它里面的按钮关闭popover。 I have tried making a jquery click function on the id "close-popover" but noting happens. 我已经尝试在id“close-popover”上创建一个jquery click函数,但是注意到了。 (I did not include my attempt to close it inside the jsfiddle) (我没有把我在jsfiddle中关闭它的尝试包括在内)

Any suggestions for how you can close a popover with a button inside the popover? 有关如何使用弹出窗口内的按钮关闭弹出窗口的任何建议?

Regards, Bill 问候,比尔

Try this:- http://jsfiddle.net/6hkkk/ 试试这个: - http://jsfiddle.net/6hkkk/

var elem = '<div class="well"><a href="google.com">Message one, From someone.</a></div>'+
    '<div class="well"><a href="google.com">Message one, From someone.</a></div>'+
    '<button id="close-popover" data-toggle="clickover" class="btn btn-small btn-primary pull-right" onclick="$(&quot;#meddelanden&quot;).popover(&quot;hide&quot;);">Close please!</button>';



$('#meddelanden').popover({animation:true, content:elem, html:true});

How about just a little onclick: 只需要一点点onclick:

<button onclick="$('#meddelanden').popover('hide');" class="btn btn-small btn-primary pull-right">Close please!</button>

Or how about a function: 或者功能怎么样:

<button onclick="close_please();" class="btn btn-small btn-primary pull-right">Close please!</button>

with... 与...

function close_please() {
    $('#meddelanden').popover('hide');
}

Or how about binding to the button after it has been created. 或者在创建按钮后如何绑定按钮。

$('#meddelanden').popover({animation:true, content:elem, html:true});
$('#close-popover').bind('click', function(){
    $('#meddelanden').popover('hide');
});

Previous examples have two main drawbacks: 以前的示例有两个主要缺点:

  1. The 'close' button needs in some way, to be aware of the ID of the referenced-element. “关闭”按钮需要以某种方式,以了解被引用元素的ID。
  2. The need of adding on the 'shown.bs.popover' event, a 'click' listener to the close button; 需要添加'shown.bs.popover'事件,关闭按钮的'click'监听器; which is also not a good solution because of, you would then be adding such listener each time the 'popover' is shown. 这也不是一个好的解决方案因为,每次显示'popover'时你都会添加这样的监听器。

Below is a solution which has not such drawbacks. 以下是没有这些缺点的解决方案。

By the default, the 'popover' element is inserted immediately after the referenced-element in the DOM (then notice the referenced-element and the popover are immediate sibling elements). 默认情况下,'popover'元素紧接在DOM中的referenced-element之后插入(然后注意引用的元素和popover是直接的兄弟元素)。 Thus, when the 'close' button is clicked, you can simply look for its closest 'div.popover' parent, and then look for the immediately preceding sibling element of such parent. 因此,当单击“关闭”按钮时,您可以简单地查找其最接近的“div.popover”父级,然后查找此父级的前一个兄弟元素。

Just add the following code in the 'onclick' handler of the 'close button: 只需在“关闭”按钮的“onclick”处理程序中添加以下代码:

$(this).closest('div.popover').prev().popover('hide');

Example: 例:

var genericCloseBtnHtml = '<button onclick="$(this).closest(\'div.popover\').prev().popover(\'hide\');" type="button" class="close" aria-hidden="true">&times;</button>';

$loginForm.popover({
    placement: 'auto left',
    trigger: 'manual',
    html: true,
    title: 'Alert' + genericCloseBtnHtml,
    content: 'invalid email and/or password'
});

try this: 试试这个:

<input type="button" onclick="$(this).parent().hide();" value="close">

by passing $(this) your referencing the button and parent references the element the button sits in 通过传递$(this)引用按钮,父引用按钮所在的元素

Alternative without calling function on click: 单击时不调用函数的替代方法:

Javascript 使用Javascript

var $popoverParent = $('.popover-parent').popover();

//allow to close when close button clicked
//register listener before popover shown
$popoverParent.on('shown.bs.popover', function()
{
    //get the actual shown popover
    var $popover = $(this).data('bs.popover').tip();

    //find the close button
    var $closeButton = $popover.find('.close-btn');

    $closeButton.click(function(){
        $popoverParent.popover('hide');
    });
});

//show your popover
$popoverParent.popover('show');

Put this in your title popover constructor... 把它放在你的标题popover构造函数中......

'<button class="btn btn-danger btn-xs pull-right"
onclick="$(this).parent().parent().parent().hide()"><span class="glyphicon
glyphicon-remove"></span></button>some text'

...to get a small red 'x' button on top-right corner ...在右上角有一个小的红色'x'按钮

//$('[data-toggle=popover]').popover({title:that string here})

i give the same answer https://stackoverflow.com/a/23556160/3603692 I hope that is allowed. 我给出相同的答案https://stackoverflow.com/a/23556160/3603692我希望这是允许的。

I have many popovers on my site, and they all have the same custom title bar, so they all have the same close button. 我的网站上有很多弹出窗口,它们都有相同的自定义标题栏,所以它们都有相同的关闭按钮。 Therefore, I can't pass an ID to an onclick handler. 因此,我无法将ID传递给onclick处理程序。 Instead, I decided to search for the open popover (only one can be open at a time) and then close it. 相反,我决定搜索打开的popover(一次只能打开一个),然后关闭它。

function closeMe() {
  $( document ).find('.popoverIsOpen').popover( 'hide' );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM