1 回答

TA貢獻(xiàn)1789條經(jīng)驗(yàn) 獲得超8個(gè)贊
您只需將data-*屬性添加到模式的主容器中即可稍后使用。
<div id="myModal" class="modal" data-foo="bar"> <!-- ... --> </div>
// Get the modal
var modal = document.getElementById("myModal");
/*...*/
// When the user clicks the button, open the modal
btn.onclick = function() {
modal.style.display = "block";
// append data-foo value to the modal's content
modal.querySelector('.modal-content p').innerText = modal.dataset.foo;
}
以下是數(shù)據(jù)集如何工作的示例: https: //codepen.io/kmsdevnet/pen/ExggvyP
數(shù)據(jù)集文檔: https://developer.mozilla.org/en-US/docs/Web/API/HTMLOrForeignElement/dataset
添加回答
舉報(bào)