javascript - bootstrap Popover 关闭按钮不起作用

标签 javascript html css twitter-bootstrap

我正在尝试使用 popover 但问题是关闭按钮在 data-content 中不起作用,我已经尝试将其放入 title 并且工作正常,但我真的需要把它放在 data-content 中 :)
谢谢大家...

$(function () {
        $('[data-toggle="popover"]').popover();
        
});
.btn {
  margin:80px
}

body {
  background: #f5f5f5
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

<button class="btn btn-success btn-edit"
                                                            data-toggle="popover"
                                                            data-html='true' data-placement="bottom"
                                                            data-content='<button type="button" id="close" class="close" onclick="$(&quot;.btn-edit&quot;).popover(&quot;hide&quot;);">&times;</button>'>
                                                        CLICK ME
                                                    </button>

最佳答案

HTML 实体 (") 不会在 JavaScript 表达式中被解析,这就是你在 onlick 属性中所拥有的。但是您可以使用反引号代替单引号。即:

<button data-content='<button onclick="$(`.btn-edit`).popover(`hide`);">...</button>'>

$(function () {
        $('[data-toggle="popover"]').popover();
        
});
.btn {
  margin:80px
}

body {
  background: #f5f5f5
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

<button class="btn btn-success btn-edit"
                                                            data-toggle="popover"
                                                            data-html='true' data-placement="bottom"
                                                            data-content='<button type="button" id="close" class="close" onclick="$(`.btn-edit`).popover(`hide`);">&times;</button>'>
                                                        CLICK ME
                                                    </button>

关于javascript - bootstrap Popover 关闭按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49287387/

相关文章:

c# - 如何用javascript按下按钮

javascript - 单击鼠标更改(切换)html 按钮的图像(在 Internet Explorer 中)

javascript - 单击特定项目时执行js函数

javascript - 将函数 random() 与 sass 一起使用来选择随机变量

javascript - 如何仅将选定列表中的 1 个值发布到 Controller 代码

html - 如何在将单个网页向下滚动到不同部分时更改导航栏上的突出显示颜色

javascript - 在打开下一个抽屉之前关闭前一个抽屉 - Twitter bootstrap 3

javascript - 原始数组已更改,未进行任何修改

javascript - 根据滚动动态调整div高度

javascript - 基于下拉选择的信息框