简体   繁体   中英

Hide and show <p> element with javascript

I am creating a text based RPG with JavaScript. I want to hide and show a <p> element when a specific input is made.

You can use the CSS style display: none; to hide elements with out removing them from document.

Add the following CSS style with a selector of p.hidden to your HTML file or an external style sheet.

<style>
   p.hidden {
      display: none;
   }
</style>

In your HTML file you can set Class="hidden" to all <p> elements that you do not want to display.

<h1>This is a visible heading</h1>
   <p> class="hidden">text here will not be displayed</p>

To achieve your expected result, use below

HTML:

<input type="text" maxlength="1">
<p>No, I dont want to kill you. Why are you here? <br>Prisoner: I dont know, I got captored. You need to help me. You need to find the key to the handcuffs. I am not sure where they are. </p>

JS

$(document).ready(function(){
  $('input').keypress(function(e) { 
    if(e.keyCode  == 13){

      if($('input').val()=='1'){
        $("p").toggle();
      }else{
        $("p").toggle();
      }
  }
    });
});

http://codepen.io/nagasai/pen/Krkxdg

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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