简体   繁体   English

使用Javascript EventListener显示元素

[英]Display elements using Javascript EventListener

I'm making a visual novel in HTML and these are my HTML and JS codes for now. 我正在用HTML制作视觉小说,这些是我现在的HTML和JS代码。

HTML: HTML:

<table border="1">
    <tr><td width="650px" align = "justify"><p id = "firstp"></p></td></tr>
    <tr><td width="650px" align = "justify"><p id = "secondp"></p></td></tr>
</table>
<embed src="assets/Haze.mp3" hidden="true" autostart="true" loop="true"></embed>

JS: JS:

    var i = 0;
    document.addEventListener("click", function(){
            if (i==1) { document.getElementById("secondp").innerHTML = "First Paragraph."; i=2;}
            if (i==0) { document.getElementById("firstp").innerHTML = "Second Paragraph."; i=1;}
        });

My questions: 我的问题:

  1. Is it possible to display paragraphs without using .innerHTML ? 是否可以不使用.innerHTML而显示段落?

Given the codes, when the user clicks his/her mouse, the paragraphs will display one by one. 给定密码,当用户单击鼠标时,段落将一一显示。 It worked. 有效。 However, what I want is instead of writing the paragraph on the JS side, would it be possible to write it between the <p> and </p> tags and use the script to simply display the text? 但是,我想要的不是在JS端编写该段落,而是可以在<p></p>标记之间编写该段落,并使用脚本简单地显示文本吗? So, HTML should now look like this: 因此,HTML现在应如下所示:

<table border="1">
    <tr><td width="650px" align = "justify"><p id = "firstp">First Paragraph</p></td></tr>
    <tr><td width="650px" align = "justify"><p id = "secondp">Second Paragraph</p></td></tr>
</table>

How should the JS change such that it could still display the paragraphs one by one whenever the user clicks his/her mouse? JS应该如何更改,以使每当用户单击鼠标时仍可以一一显示段落?

  1. How to apply #1 method on displaying other elements (ie buttons, links, pictures)? 如何在显示其他元素(即按钮,链接,图片)时应用#1方法?

It's just the same as in number 1 but instead of a paragraph, it will display the button/link/picture when user clicks his/her mouse. 它与数字1相同,但不是段落,而是当用户单击鼠标时将显示按钮/链接/图片。 I'm not referring to the onclick event. 我指的不是onclick事件。 I'm still referring to EventListener . 我仍然指的是EventListener

  1. How to change background music ( embed ) when mouse is clicked? 单击鼠标时如何更改背景音乐( embed )? Or keep the music playing even when player/user progressed to the next scene (meaning different HTML)? 还是即使播放器/用户前进到下一个场景(意味着使用不同的HTML),也可以继续播放音乐?

I thought of putting the music outside the iframe just to loop the music. 我想到将音乐放到iframe之外只是为了循环播放音乐。 But I figured doing so would be useless, because what if in the next scene, I want to use a different background music. 但是我认为这样做是没有用的,因为如果在下一场景中我想使用其他背景音乐该怎么办。 Or is it possible for the HTML within the iframe to trigger and change the music playing outside the iframe ? 还是iframe的HTML可能触发并更改iframe外部播放的音乐?

try using JQuery, also add some class name(eg class1 ) then load the below script 尝试使用JQuery,还添加一些类名(例如class1 ),然后加载以下脚本

$(document).ready(function(){
    $('.class1').hide();
    $('.class1').click(function(){
        $(this).hide();
    });
    $('.class2').click(function(){
        $(this).next().toggle();
    });
});

here is the demo 这是演示

try using this: EDITED 尝试使用此: 编辑

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<table border="1">
    <tr><td width="650px" align = "justify"><p id = "firstp">First Paragraph</p></td></tr>
    <tr><td width="650px" align = "justify"><p id = "secondp">Second Paragraph</p></td></tr>
</table>


<div id="music">
    <embed type="audio/mpeg" src="#" name="plugin" height="100%" width="100%" id="mplayer">
</div>  
<script>
$(document).ready(function(){
    $("#firstp").hide();
    $("#secondp").hide();
    var i = 0;
var songs = [];
songs[0] = "/XXXX/../assets/Hangover - DownloadMing.SE.mp3";// full path to your songs if the are not in current directory
songs[1] = "/XXXX/../assets/Tu Hi Tu - DownloadMing.SE.mp3";
    document.addEventListener("click", function(){
        if (i==1) { $("#secondp").show();}
        if (i==0) { $("#firstp").show(); i = 1;}
  $('#mplayer').attr('src',songs[j%2]);
j++;
    });

  });  
  </script>

note: For buttons also you can create them on your page and with the button id hide them and show them on addEventListener of click. 注意:对于按钮,您也可以在页面上创建它们,并使用按钮ID隐藏它们并将其显示在click的addEventListener上。

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

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