简体   繁体   English

如何通过单击 div 切换 div 可见性?

[英]How to toggle div visibility by clicking a div?

I'm trying to do a little visual novel game with multiple choice buttons, that will slightly or more change the story, images, text boxes, and such... and without javascript it's like... Quite impossible.我正在尝试做一个带有多项选择按钮的视觉小说小游戏,它会稍微或更多地改变故事、图像、文本框等等……如果没有 javascript,它就像……完全不可能。

Or possible, but it would require dozen of thousands of pages.或者可能,但它需要几十万页。 Who knows.谁知道。 I want to avoid that.我想避免这种情况。

So...所以...

Is there a way to replace, for multiple times (also 100 times when required) the content of a visible div - id="d01" for example - including javascript and css - with the content of another similar but invisible div, making the invisible div visible, hiding the previous, but clicking on the div itself?有没有办法多次(在需要时也可以替换 100 次)可见 div 的内容 - 例如id="d01" - 包括 javascript 和 css - 用另一个类似但不可见的 div 的内容,使不可见div 可见,隐藏前一个,但点击 div 本身?

I've been able to find only toggles and replacements that will work for only one time, and then go back to the first content, but I'm searching for something a bit different.我只能找到只能使用一次的切换和替换,然后返回到第一个内容,但我正在寻找一些不同的东西。

Something that changes progressively, from div one to div 100 or more.逐渐变化的东西,从 div 1 到 div 100 或更多。

I'm working from a smartphone, so, compilers or visual novel editors are out of reach for me.我正在使用智能手机工作,因此编译器或视觉小说编辑器对我来说遥不可及。 But usually websites and other little things, also with a bit of java too, worked for me.但通常网站和其他小东西,也有一些 Java,对我有用。 So i think it will be no big fuss.所以我认为这不会有什么大惊小怪的。

I know everything is a bit confusing.我知道一切都有些混乱。 Sorry for that.对不起。

<div id="d01">

<!-- Content to show at the beginning, with everything it could come
to my mind: text, buttons, images, animations... ; -->

</div>
<div id="d02" style="display:none;">

<!-- Content to show after the first click, similar to the first, but
with light changes: other text, other characters, other images... ; -->

</div>
<div id="d03" style="display:none;">

<!-- Content to show after the second click, like as above; -->

</div>
<div id="d0#"> style="display:none;">

<!-- Content to show after the n. click... U got it at this point, i think; -->

</div>

Instead having a lot of div in the HTML, you can just use a single div and dynamically replace its content using javascript代替在 HTML 中有很多div ,您可以只使用单个div并使用 javascript 动态替换其内容

First, create a div element in the html and lets put dialogBox as its id首先,在 html 中创建一个 div 元素,并让dialogBox作为它的 id

<div id="dialogBox">
  
</div>

In javascript, declare the dialogs as an array like so在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;"
  }
];

It is up to you what to put in there but in this case, I just put text and id .在那里放什么取决于你,但在这种情况下,我只放了textid id isn't really necessary at this moment but we can use it for querying since there will hundreds of this in the future. id目前并不是必需的,但我们可以使用它进行查询,因为将来会有数百个。

Now, find and store our div element into a variable现在,找到我们的div元素并将其存储到一个变量中

const dialogBox = document.getElementById('dialogBox');

Finally, we can now update/replace our div content.最后,我们现在可以更新/替换我们的div内容。 In this example, we'll be updating the content on each mouse clicks在这个例子中,我们将在每次鼠标点击时更新内容

let index = 0;
dialogBox.innerHTML = dialogs[index].text

window.addEventListener('click', () => {
  index = (index < dialogs.length - 1) ? ++index : index;
  dialogBox.innerHTML = dialogs[index].text
});

In the above example, we set the div innerHTML which its content, to the current dialog text.在上面的示例中,我们将其内容的div innerHTML 设置为当前对话框文本。 Then, increment the index value by 1 on each mouse click then update the innerHTML again.然后,每次单击鼠标时将index值增加 1,然后再次更新 innerHTML。

And we are done!我们完成了!

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

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