我有一系列按鈕,其中包含我想插入到它們切換的彈出視窗中的資料屬性。
HTML:
<button type="button" class="btn btn-yellow btn-deleteTeacher" data-bs-toggle="popover" role="button" data-username="farkle86">
<i class="fa-solid fa-circle-xmark"></i>
</button>
JS:
$('[data-bs-toggle="popover"]').popover({
html: true,
container: 'body',
placement: 'top',
content: "<a href='teachers.asp?action=delete&user=" $(this).data('username') "'>Click Here</a> to permanently delete this teacher."
});
當我將滑鼠懸停在彈出視窗中的文本鏈接上時,變數為“未定義”。我不知道如何正確獲取用戶名資料屬性。
uj5u.com熱心網友回復:
問題是因為this在popover()宣告中不是該data屬性所在的元素。要獲得對元素的參考,您需要在宣告彈出框時手動遍歷它們:
$('[data-bs-toggle="popover"]').each((i, el) => {
let $el = $(el);
$el.popover({
html: true,
container: 'body',
placement: 'top',
content: `<a href="teachers.asp?action=delete&user=${$el.data('username')}">Click Here</a> to permanently delete this teacher.`
});
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/507460.html
