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

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

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

javascripttwitter-bootstrap

提问by apscience

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

回答by Esailija

In https://inputs.io/js/buttons.jsthe jQuery plugin jQuery.fn.popoveris 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")此时不再调用引导程序,而是调用由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()

回答by Anton

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

回答by Salman

Replace

代替

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

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

with

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

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

Works for me in the console.

在控制台中对我来说有效。

回答by Undefined Behavior

try to move id="smiley"from

尝试id="smiley"

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

<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;">

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

回答by sulfureous

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.

当我点击笑脸时,它会关闭弹出窗口,然后在运行第二个代码块之前我无法再次打开它。它非常接近,但我不确定我到底错过了什么。

回答by Farhad Khairzad

If you have a selector property (e.g., 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');

回答by Vidhuran

In the addSmileyfunction you could replace the

addSmiley函数中,您可以替换

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

with

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

It would work , but don't know if it is ok for you.

它会起作用,但不知道它是否适合你。