无法将 javascript 中的 bootstrap 弹出窗口添加到动态创建的元素
cannot add bootstrap popover from javascript to dynamically created elements
我正在使用 javascript 在 HTML 中动态生成卡片数组,目前,当用户单击此按钮时,我会放入每个卡片按钮,他应该能够看到弹出窗口,但在此之前,我获取了一些数据以放在卡片上,但出现错误或丢失,弹出窗口没有出现,如果我评论了 loadPhotos() 的第一行,它就可以正常工作。
$(function () {
$('[data-toggle="popover"]').popover()
})
const getAllPhotos = async () => {
let res = await fetch('https://jsonplaceholder.typicode.com/photos').then(
response => { return response.json() }
)
return res;
}
const displayPhotos = (photos) => {
const view = document.getElementById('photosGrid');
photos.forEach(element => {
view.append(element)
});
}
const createCard = (data) => {
const card = document.createElement('div');
card.classList.add('card', 'col-lg-2', 'm-2', 'p-0')
const optionsButton = document.createElement('button')
optionsButton.innerText = "test"
optionsButton.title = "title of me"
optionsButton.setAttribute('data-toggle', 'popover')
optionsButton.setAttribute('data-content', 'content of me')
card.appendChild(optionsButton)
return card;
}
const createCardsArray = async (data) => {
const res = await data.map(d => {
return createCard(d);
})
return res;
}
const loadPhotos = async () => {
const photos = await getAllPhotos();
const cards = await createCardsArray([1, 2]);
displayPhotos(cards);
}
loadPhotos();
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<title>Pinterest Homepage</title>
</head>
<body>
<div class="container-fluid">
<div class="row" id="photosGrid">
</div>
</div>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"
integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"
integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
crossorigin="anonymous"></script>
<script type="text/javascript" src="index.js"></script>
</body>
</html>
创建元素后初始化popover,如果后面添加更多元素,可以重新初始化popover。
JQuery 在初始化期间将弹出窗口添加到元素,因此初始化后创建的元素不会由 JQuery
分配其弹出窗口
我正在使用 javascript 在 HTML 中动态生成卡片数组,目前,当用户单击此按钮时,我会放入每个卡片按钮,他应该能够看到弹出窗口,但在此之前,我获取了一些数据以放在卡片上,但出现错误或丢失,弹出窗口没有出现,如果我评论了 loadPhotos() 的第一行,它就可以正常工作。
$(function () {
$('[data-toggle="popover"]').popover()
})
const getAllPhotos = async () => {
let res = await fetch('https://jsonplaceholder.typicode.com/photos').then(
response => { return response.json() }
)
return res;
}
const displayPhotos = (photos) => {
const view = document.getElementById('photosGrid');
photos.forEach(element => {
view.append(element)
});
}
const createCard = (data) => {
const card = document.createElement('div');
card.classList.add('card', 'col-lg-2', 'm-2', 'p-0')
const optionsButton = document.createElement('button')
optionsButton.innerText = "test"
optionsButton.title = "title of me"
optionsButton.setAttribute('data-toggle', 'popover')
optionsButton.setAttribute('data-content', 'content of me')
card.appendChild(optionsButton)
return card;
}
const createCardsArray = async (data) => {
const res = await data.map(d => {
return createCard(d);
})
return res;
}
const loadPhotos = async () => {
const photos = await getAllPhotos();
const cards = await createCardsArray([1, 2]);
displayPhotos(cards);
}
loadPhotos();
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<title>Pinterest Homepage</title>
</head>
<body>
<div class="container-fluid">
<div class="row" id="photosGrid">
</div>
</div>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"
integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"
integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
crossorigin="anonymous"></script>
<script type="text/javascript" src="index.js"></script>
</body>
</html>
创建元素后初始化popover,如果后面添加更多元素,可以重新初始化popover。
JQuery 在初始化期间将弹出窗口添加到元素,因此初始化后创建的元素不会由 JQuery
分配其弹出窗口