当我点击一个动态创建的Font Awesome图标时,我遇到了一些触发弹出的问题。
index.ejs
<!-- Template for Snazzy Window -->
<script id="marker-content-template" type="text/x-handlebars-template">
<div class="custom-img" style="background-image: url({{{bgImg}}})"></div>
<section class="custom-content">
<h1 class="custom-header">
{{title}} <i class="fa fa-question-circle" data-placement="right" data-toggle="popover" data-container="body" data-content="And here's some amazing content. It's very engaging. Right?"></i>
<small>{{governance}}</small>
</h1>
<div class="custom-body">{{{body}}}</div>
</section>
</script>
<script type="text/javascript>
$(function() {
var template = Handlebars.compile($('#marker-content-template').html());
});
</script>我尝试过各种方法,包括
$(document).on('click', '.fa', function(){
$('[data-toggle="popover"]').popover('toggle');
});这不起作用,现在我在想这是不是和Handlebar模板有关?
我该如何处理这个问题呢?
发布于 2017-03-04 23:46:27
当您运行
$(document).on('click', '.fa', function(){
$('[data-toggle="popover"]').popover('toggle');
});handlebar创建了元素,但没有将其添加到DOM中。
所以在你的代码中
// 1. Create the element from template with handlebar
var template = Handlebars.compile($('#marker-content-template').html());
// 2. Add the element to the DOM
document.getElementById('#yourTargetContainerId').innerHTML = template;
// 3. add the eventlistener to the elements
$(document).on('click', '.fa', function(){
$('[data-toggle="popover"]').popover('toggle');
});https://stackoverflow.com/questions/42597855
复制相似问题