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

时间:2019-03-17 21:29:53

标签: javascript jquery html popover

我在W3学校网站上有这种自举弹出窗口,并针对我的情况编辑了代码(请参见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>

  

问题是,点击后如何更改弹出框的HTML数据内容   弹出按钮/触发,好吗?有没有像   onHtmlDomPopoverLoad?

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

2 个答案:

答案 0 :(得分:3)

您可以将onPopoverHtmlLoad替换为:

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

并将事件处理程序附加到:

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

请参阅codepen:https://codepen.io/anon/pen/MxXwQe

答案 1 :(得分:1)

在此处了解有关引导程序弹出事件的信息: 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>