如何通过 Javascript 在 Popover 内容 (Bootstrap) 中添加 Html 标签(如 <button>)
How can I add Html tags (like <button>) in Popover Content (Bootstap) by Javascript
我正在使用 Bootstrap-Popover。我想在弹出框内有一个按钮。之前还好好的,现在不行了
<button type="button" class="btn btn-primary mx-2" id="pop" data-container="body" data-toggle="popover" data-placement="bottom" data-html="true" data-content="">
Button
</button>
Javascript:
function updatePopover() {
var popStr = "";
popStr = popStr + "<h5> Heading : </h5><div class='mx-2 my-2'>";
popStr = popStr + "<button class='btn btn-success' onclick='function()' id ='funcid'>Button</button>"
document.getElementById('pop').setAttribute('data-content', popStr);
$('#popcart').popover('hide');
}
您提供的代码已损坏。你忘记了 "
在
的结尾
popStr = popStr + "<button class='btn btn-success' onclick='function()' id ='funcid'>Button</button>"
你能试试看它是否解决了你的问题。
简单地说,您可以执行以下操作:
var dynamicData = ['Dynamic Data', 'Dynamic Data', 'Dynamic Data', 'Dynamic Data'];
$('[data-toggle="popover"]').popover({
title: "<h5>Heading</h5>",
content: function () {
var resultData = $(`<div><ul class="list-group">`);
for (var i in dynamicData) {
resultData = resultData.add(
`<li class="list-group-item d-flex justify-content-between align-items-center">
`+ dynamicData[i] + `
<span class="badge badge-primary badge-pill">`+ i + `</span>
</li>`
);
// console.log(i, dynamicData[i]);
}
resultData = resultData.add(`</ul><br/><button class='btn btn-danger' id ='funcid'>Close</button></div>`);
return resultData;
}
});
$(document).on("click", "#funcid", function () {
$(this).parents(".popover").popover('hide');
});
.popover-body {
width: 200px;
}
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"
integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"
integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<button type="button" class="btn btn-primary mx-2" data-toggle="popover" data-placement="bottom" data-html="true">
Button
</button>
我正在使用 Bootstrap-Popover。我想在弹出框内有一个按钮。之前还好好的,现在不行了
<button type="button" class="btn btn-primary mx-2" id="pop" data-container="body" data-toggle="popover" data-placement="bottom" data-html="true" data-content="">
Button
</button>
Javascript:
function updatePopover() {
var popStr = "";
popStr = popStr + "<h5> Heading : </h5><div class='mx-2 my-2'>";
popStr = popStr + "<button class='btn btn-success' onclick='function()' id ='funcid'>Button</button>"
document.getElementById('pop').setAttribute('data-content', popStr);
$('#popcart').popover('hide');
}
您提供的代码已损坏。你忘记了 "
在
popStr = popStr + "<button class='btn btn-success' onclick='function()' id ='funcid'>Button</button>"
你能试试看它是否解决了你的问题。
简单地说,您可以执行以下操作:
var dynamicData = ['Dynamic Data', 'Dynamic Data', 'Dynamic Data', 'Dynamic Data'];
$('[data-toggle="popover"]').popover({
title: "<h5>Heading</h5>",
content: function () {
var resultData = $(`<div><ul class="list-group">`);
for (var i in dynamicData) {
resultData = resultData.add(
`<li class="list-group-item d-flex justify-content-between align-items-center">
`+ dynamicData[i] + `
<span class="badge badge-primary badge-pill">`+ i + `</span>
</li>`
);
// console.log(i, dynamicData[i]);
}
resultData = resultData.add(`</ul><br/><button class='btn btn-danger' id ='funcid'>Close</button></div>`);
return resultData;
}
});
$(document).on("click", "#funcid", function () {
$(this).parents(".popover").popover('hide');
});
.popover-body {
width: 200px;
}
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"
integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"
integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<button type="button" class="btn btn-primary mx-2" data-toggle="popover" data-placement="bottom" data-html="true">
Button
</button>