简体   繁体   English

无法让 Bootstrap Popover 处理生成的卡片

[英]Can't get Bootstrap Popover to work on generated Cards

I have a simple search website that generates Bootstrap Cards with artist name, the cover art, and album title that corresponds to search terms.我有一个简单的搜索网站,可以生成带有艺术家姓名、封面艺术和与搜索词相对应的专辑标题的 Bootstrap 卡片。 I want to include the "dismiss on next click" popover when you click the album cover art image that displays a list of songs for the album or the back cover art.当您单击显示专辑或封底艺术的歌曲列表的专辑封面图片时,我想包括“在下一次点击时关闭”弹出窗口。 I've gotten the popover to work with simple text that is in my HTML file but when I apply the same code in my JS file to the Card it doesn't work.我已经让弹出框处理 HTML 文件中的简单文本,但是当我将 JS 文件中的相同代码应用于卡片时,它不起作用。

Here is the snippet of my function for outputting the card, taking information that loops through an array of objects.这是我输出卡片的函数片段,获取循环对象数组的信息。

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

function outputResults() {
  outputDiv.append(`<div class="card album bg-dark text-white col" style="width:18rem;"><h5 class="card-header">${i.artist}</h5>
    <a tabindex="0" data-trigger="focus" title="SONGS" data-toggle="popover"  data-content="THIS IS THE INFO I WANT TO SHOW IN THE POPOVER"><img class="card-img-top" src="${i.cover}" alt="${i.title}"></a>
    <div class="card-footer"><a target = "_blank" href="${i.link}">"${i.title}"</a> -${i.year}</div></div>`).children(':last').hide().fadeIn(1500);
};

I believe it has something to do with the popover not being present in the html when the page loads but is generated for each individual card once a button is pressed.我相信这与页面加载时弹出窗口不存在于 html 中有关,而是在按下按钮后为每张卡片生成。

This is the bit of code that I created in my HTML file that the popup works on:这是我在弹出窗口工作的 HTML 文件中创建的代码:

<a tabindex="0" data-trigger="focus" title="Header" data-toggle="popover" data-content="Some content">Click Me</a>

I think you should try to use toggle() function on the popover if data-toggle is not working.如果 data-toggle 不起作用,我认为您应该尝试在 popover 上使用 toggle() 函数。

I think you want something like this.我想你想要这样的东西。 if not then tell.Same thing done with using button click如果没有,那么告诉。使用按钮单击完成相同的事情

 $(document).ready(function() { $('[data-toggle="popover"]').popover() }); outputResults(); function outputResults() { $("#outputDiv").append(`<div class="card album bg-dark text-white col" style="width:18rem;"> <h5 class="card-header"></h5> <a tabindex="0" data-trigger="focus" title="SONGS" data-toggle="popover" data-content="THIS IS THE INFO I WANT TO SHOW IN THE POPOVER"><img class="card-img-top" >click dynamic</a> <div class="card-footer"><a target = "_blank" href=""</a> </div></div>`); }; $("#btn").on('click',function(){ $('a').popover("show"); });
 <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><a tabindex="0" data-trigger="focus" title="Header" data-toggle="popover" data-content="Some content">Click Me</a><br><br> <BR><br><br> <BR><br><br> <BR><br><br> <BR> <div id=outputDiv></div> <button id=btn>click here </button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM