2017-07-30 53 views
0

林做了以下酥料餅: https://jsfiddle.net/ca4h0a0q/引導酥料餅,關閉按鈕

<a class='danger' data-placement='above' title="Popover Title" href='#'>Click</a> 
<div id="popover_content_wrapper" style="display: none"> 
<button class="close"> 
    x 
    </button> 
    <div>This is your div content</div> 
</div> 

的Javascript:

$(document).ready(function(){ 
    $("body").on("click",".close",function(){ 
    $(".danger").trigger("click"); 
    }); 

    $('.danger').popover({ 
    html : true, 
    content: function() { 
     return $('#popover_content_wrapper').html(); 
    } 
    }); 
}); 

爲了閉合酥料餅的,由於這個錯誤:https://github.com/twbs/bootstrap/issues/16732我不得不觸發click事件在打開它的按鈕上。我不太喜歡這個解決方案。還有更好的方法嗎?

+0

檢查這個問題,有各種方式來添加關閉按鈕酥料餅講到這裏,https://stackoverflow.com/questions/ 13413057/How-to-insert-close-button-in-popover-for-bootstrap – azs06

回答

0

我將在closest替換此

$("body").on("click",".close",function(){ 
    $(".danger").trigger("click"); 
}); 

有了這個

$("body").on("click",".close",function(){ 
    $(this) 
     .closest("a.danger") 
     .trigger("click"); 
}); 

更多:如果你有多個 popovers或與該類danger元素http://api.jquery.com/closest/

這樣,他們也不會被觸發。

+0

@JoelAlvarezMyrrie我的回答能幫助你嗎?讓我知道你是否需要更多的幫助 – jeanfrg

1

我是這樣工作的。

<div id="popover_content_wrapper" style="display: none"> 
    <button class="close" onclick="$('.danger').popover('hide').trigger('click')"> 
    x 
    </button> 
<div>This is your div content</div> 

而且JS ...

$(document).ready(function(){ 
    $('.danger').popover({ 
    html : true, 
    content: function() { 
     return $('#popover_content_wrapper').html(); 
    } 
    }); 
});