How to prevent the popover div for hiding on clicking inside it for twitter bootstrap “dismissible popover”(data-trigger=“focus”)?

做~自己de王妃 提交于 2019-12-04 01:44:44

Clicks on the popover are hiding that popover because they are stealing the browser's focus. As soon as the button loses focus, the popover will be hidden (because we've set data-trigger="focus").

To address exact question you asked:

prevent the popover div for hiding on clicking inside it for twitter bootstrap “dismissible popover”(data-trigger=“focus”)?

The simplest way to fix this is to prevent clicks inside the popover from stealing focus by:

  • Adding an event listener to catch clicks inside the popover
  • Calling preventDefault() on the caught event

This will stop the focus from being stolen, and so stop the popover from being closed.

Note: We need to add the event listener on mousedown rather than click, because that is when the focus is set by the browser.

A more in-depth explanation of why we use mousedown and preventDefault() can be found on this StackOverflow answer: Prevent firing focus event when clicking on div

$(function() {
  $('[data-toggle="popover"]').popover()
})

$('body')
  .on('mousedown', '.popover', function(e) {
    console.log("clicked inside popover")
    e.preventDefault()
  });
<html>

<body>
  <script src="https://code.jquery.com/jquery.min.js"></script>
  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  <a tabindex="0" class="btn btn-danger" data-toggle="popover" data-trigger="focus" title="Dismissible" data-content="Clicks in here won't steal focus">
    Dismissible popover
  </a>
</body>
</html>

You should use tigger: 'click'.

$('.BookAppButton').popover({
        title : '',
        html : 'true',
    trigger: 'click',
    content:'<div style="border:black 2px solid"><p>Enter name : <input type="text"></input></p></div>'
    });
});

Instead of

data-trigger="focus"

you should use

data-trigger="'focus'"

for AngularUI Bootstrap version 2.0 and above.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!