简体   繁体   English

Bootstrap .popover('show'), .popover('hide') 不起作用。 将其绑定到点击作品

[英]Bootstrap .popover('show'), .popover('hide') not working. Binding it to click works

I have a button that has been binded to a popover.我有一个已绑定到弹出框的按钮。 I would like to hide the popover when someone clicks on one of the smilies in the popover.当有人点击弹出窗口中的一个表情符号时,我想隐藏弹出窗口。 However, $("#smiley").popover('hide') does not work.但是, $("#smiley").popover('hide')不起作用。

Unfortunately I haven't been able to reproduce this with barebones code - it only happens on the live site, which is https://coinchat.org不幸的是,我无法用准系统代码重现这一点 - 它只发生在实时站点上,即https://coinchat.org

Relevant code:相关代码:

$("#smiley").popover({html: true, trigger: 'click', placement: 'top', content: smileyContent, title: 'Smilies'});

Later in a function..后来在一个函数中..

$("#smiley").popover('hide'); // not working

In https://inputs.io/js/buttons.js the jQuery plugin jQuery.fn.popover is overwritten on a load event of some kind, so $("#smiley").popover("hide") at that point is no longer calling into bootstrap but the plugin provided by inputs.io .https://inputs.io/js/buttons.js 中,jQuery 插件jQuery.fn.popover在某种加载事件上被覆盖,所以$("#smiley").popover("hide")在那一点不再调用 bootstrap 而是调用inputs.io提供的插件。

A snippet of the code:代码片段:

Inputsio.load = function(){
    (function(){(function(e){return e.fn.popover=function(t)

The usage of jQuery plugin namespace for application specific code is extremely distasteful indeed.将 jQuery 插件命名空间用于特定于应用程序的代码确实非常令人反感。

A temporary fix could be $("#smiley").click()临时修复可能是$("#smiley").click()

here is a working fiddle that is "similar" to your code这是一个与您的代码“相似”的工作小提琴

http://jsfiddle.net/6hkkk/7/ http://jsfiddle.net/6hkkk/7/

HTML HTML

<div style="margin-top:100px">
    <span id="smiley" data-title="smile" data-toggle="clickover">
        <i class="icon-comment"></i>
    </span>
</div>

javascript javascript

ClosePop = function () {
    $('#smiley').popover('hide');
}

var elem = '<button data-toggle="clickover" class="btn" onclick="ClosePop();"><i class="icon-off"></i></button>';

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

Replace代替

$("#smiley").popover('hide');

with

$("#smiley").click();

Works for me in the console.在控制台中为我工作。

try to move id="smiley" from尝试将id="smiley"

<span class="btn tenpx smileypopover popover-trigger" id="smiley" data-original-title="" title="">

to

<div class="popover fade top in" style="top: 430px; left: 308.5px; display: block;">

Is something like this out of the question?这样的事情是不可能的吗?

$('#smileylist a').click(function(){
  if($('.popover').css('display','block')){
    $(this).css('display','none');
  }
});

$('.smileypopover').click(function(){
  if ($('.popover').css('display','none')){
    $(this).css('display','block');
  }
});

When I click on the smiley face it closes the popover, then I can't open it again until I run the 2nd block of code.当我点击笑脸时,它会关闭弹出窗口,然后在运行第二个代码块之前我无法再次打开它。 It's very close but I'm not sure exactly what I'm missing.它非常接近,但我不确定我到底错过了什么。

If you have a selector property (eg, usually required for dynamic HTML content) in your popover options, be sure to use the same selector when calling the 'hide' method.如果您的弹出窗口选项中有选择器属性(例如,动态 HTML 内容通常需要),请确保在调用 'hide' 方法时使用相同的选择器。

The following fails to hide popovers (for new content added to the DOM).以下无法隐藏弹出窗口(对于添加到 DOM 的新内容)。

//enable popover
$(document).popover({
  html: true,
  selector: "[data-popover]"
});

//attempt to hide popover
$(document).popover('hide');

Instead, use this:相反,使用这个:

//enable popover
$(document).popover({
  html: true,
  selector: "[data-popover]"
});

// hide popover
$('[data-popover]').popover('hide');

Bootstrap cannot reach indepentently popover id's. Bootstrap 无法独立地访问 popover id。 We have to read aria-describedby attribute related with the element.我们必须读取与元素相关的 aria-describedby 属性。

Code below is solves your issue:下面的代码可以解决您的问题:

$("#"+$(relatedElementId).attr("aria-describedby")).remove();

relatedElementId: popover's associated element relatedElementId:popover 的关联元素

In the addSmiley function you could replace theaddSmiley函数中,您可以替换

$("#smiley").popover('hide');

with

$(".popover").hide(); 

It would work , but don't know if it is ok for you.它会工作,但不知道它是否适合你。

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

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