如何在弹出内容加载后 运行 js 函数 - 单击按钮后?

How to run js function after popover content loads - after button click?

我有这种来自 W3 学校网站的 bootstrap 弹出窗口和针对我的情况编辑的代码(参见 onPopoverHtmlLoad() 函数):

<!DOCTYPE html> 
<!-- https://www.w3schools.com/bootstrap/tryit.asp?filename=trybs_popover&stacked=h -->
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  <h3>Popover Example</h3>
  <a href="#" data-toggle="popover" data-html='true'title="Popover Header" data-content="Some content inside the popover <button id='inpopover_button'>false</button>">Toggle popover</a>
</div>

<script>
$(document).ready(function(){
    $('[data-toggle="popover"]').popover();   
});

//how to ensure to run this function on popoverHtmlDomLoad?
function onPopoverHtmlLoad(){
  document.getElementById("inpopover_button").innerHTML = "true"
}
</script>

</body>
</html>

Question is, how can I change HTML data content of popover after I hit popover button/trigger, please? Is there some function like onHtmlDomPopoverLoad?

我很欣赏 Javascript 中的解决方案,但也接受 JQuery 的帮助。我一直在寻找类似的问题,但还没有找到任何东西。

您可以将 onPopoverHtmlLoad 替换为:

function onPopoverHtmlLoad(){
    $("#inpopover_button").text("true")
}

并附加事件处理程序:

$('[data-toggle="popover"]').on('shown.bs.popover', onPopoverHtmlLoad)

查看代码笔:https://codepen.io/anon/pen/MxXwQe

在此处了解 bootstrap 弹出事件: https://getbootstrap.com/docs/4.0/components/popovers/#events

$('#myPopover').on('hidden.bs.popover', function () {
  // do something…
})

<!DOCTYPE html> 
<!-- https://www.w3schools.com/bootstrap/tryit.asp?filename=trybs_popover&stacked=h -->
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  <h3>Popover Example</h3>
  <a href="#" data-toggle="popover" data-html='true'title="Popover Header" data-content="Some content inside the popover <button id='inpopover_button'>false</button>">Toggle popover</a>
</div>

<script>
$(document).ready(function(){
    $('[data-toggle="popover"]').popover()
        .on('shown.bs.popover', onPopoverHtmlLoad);
});

//how to ensure to run this function on popoverHtmlDomLoad?
function onPopoverHtmlLoad(){
  document.getElementById("inpopover_button").innerHTML = "true"
}
</script>

</body>
</html>