1 回答

TA贡献1851条经验 获得超3个赞
div而不是在 HTML 中有很多,你可以只使用一个div并使用 javascript 动态替换它的内容
首先,在 html 中创建一个 div 元素,让我们把它dialogBox作为它的 id
<div id="dialogBox">
</div>
在javascript中,像这样将对话框声明为数组
const dialogs = [
{
id: '01',
text: "Content to show at the beginning, with everything it could come to my mind: text, buttons, images, animations... ;"
},
{
id: '02',
text: "Content to show after the second click, like as above;"
},
{
id: '03',
text: "Content to show after the n. click... U got it at this point, i think;"
}
];
放什么取决于你,但在这种情况下,我只是放text和id。id目前并不是真正必要的,但我们可以用它来查询,因为将来会有数百个。
现在,找到我们的div元素并将其存储到一个变量中
const dialogBox = document.getElementById('dialogBox');
最后,我们现在可以更新/替换我们的div内容。在这个例子中,我们将在每次鼠标点击时更新内容
let index = 0;
dialogBox.innerHTML = dialogs[index].text
window.addEventListener('click', () => {
index = (index < dialogs.length - 1) ? ++index : index;
dialogBox.innerHTML = dialogs[index].text
});
在上面的示例中,我们将div其内容的 innerHTML 设置为当前对话框文本。然后,index每次单击鼠标时将值增加 1,然后再次更新 innerHTML。
添加回答
举报