Skip to main content
adicionou 220 caracteres ao conteúdo
Fonte Link
Enio
  • 1,5mil
  • 4
  • 20
  • 34

TroqueQuando você tenta verificar com um if se document.getElementById("div").style.opacity e igual a 0 vai retornar undefined porque não vai ter nada no inicio para resolver isso você so precisa verificar se document.getElementById("div").style.opacitye igUAL a "" ou seja vazio.

Então troque a parte de seu código:

if (document.getElementById("div").style.opacity=="0") {

por

if (document.getElementById("div").style.opacity=="0" || document.getElementById("div").style.opacity=="") {

<!DOCTYPE html>
<html>
  <head>
    <title>teste</title>
    <style type="text/css">
      div{
        height: 100px;
          width: 100px;
          border: 5px solid blue;
          opacity: 0; 
    }
    </style>
  </head>
  <body>
  <script type="text/javascript">
      function vai(){
        if (document.getElementById("div").style.opacity=="0" || document.getElementById("div").style.opacity=="") {
            document.getElementById("div").style.opacity = 1;
          }
          else {
            document.getElementById("div").style.opacity = 0;
          }
    }
    </script>
    <p onClick="vai()">aparece</p>
    <div id="div">123</div>
  </body>
</html>
 Executar

Troque a parte de seu código:

if (document.getElementById("div").style.opacity=="0") {

por

if (document.getElementById("div").style.opacity=="0" || document.getElementById("div").style.opacity=="") {

<!DOCTYPE html>
<html>
  <head>
    <title>teste</title>
    <style type="text/css">
      div{
        height: 100px;
          width: 100px;
          border: 5px solid blue;
          opacity: 0; 
    }
    </style>
  </head>
  <body>
  <script type="text/javascript">
      function vai(){
        if (document.getElementById("div").style.opacity=="0" || document.getElementById("div").style.opacity=="") {
            document.getElementById("div").style.opacity = 1;
          }
          else {
            document.getElementById("div").style.opacity = 0;
          }
    }
    </script>
    <p onClick="vai()">aparece</p>
    <div id="div">123</div>
  </body>
</html>
 Executar

Quando você tenta verificar com um if se document.getElementById("div").style.opacity e igual a 0 vai retornar undefined porque não vai ter nada no inicio para resolver isso você so precisa verificar se document.getElementById("div").style.opacitye igUAL a "" ou seja vazio.

Então troque a parte de seu código:

if (document.getElementById("div").style.opacity=="0") {

por

if (document.getElementById("div").style.opacity=="0" || document.getElementById("div").style.opacity=="") {

<!DOCTYPE html>
<html>
  <head>
    <title>teste</title>
    <style type="text/css">
      div{
        height: 100px;
          width: 100px;
          border: 5px solid blue;
          opacity: 0; 
    }
    </style>
  </head>
  <body>
  <script type="text/javascript">
      function vai(){
        if (document.getElementById("div").style.opacity=="0" || document.getElementById("div").style.opacity=="") {
            document.getElementById("div").style.opacity = 1;
          }
          else {
            document.getElementById("div").style.opacity = 0;
          }
    }
    </script>
    <p onClick="vai()">aparece</p>
    <div id="div">123</div>
  </body>
</html>

adicionou 220 caracteres ao conteúdo
Fonte Link
Enio
  • 1,5mil
  • 4
  • 20
  • 34

Troque a parte de seu código:

if (document.getElementById("div").style.opacity=="0") {

por

if (document.getElementById("div").style.opacity=="0" || document.getElementById("div").style.opacity=="") {

<!DOCTYPE html>
<html>
  <head>
    <title>teste</title>
    <style type="text/css">
      div{
        height: 100px;
          width: 100px;
          border: 5px solid blue;
          opacity: 0; 
    }
    </style>
  </head>
  <body>
  <script type="text/javascript">
      function vai(){
        if (document.getElementById("div").style.opacity=="0" || document.getElementById("div").style.opacity=="") {
            document.getElementById("div").style.opacity = 1;
          }
          else {
            document.getElementById("div").style.opacity = 0;
          }
    }
    </script>
    <p onClick="vai()">aparece</p>
    <div id="div">123</div>
  </body>
</html>
 Executar

<!DOCTYPE html>
<html>
  <head>
    <title>teste</title>
    <style type="text/css">
      div{
        height: 100px;
          width: 100px;
          border: 5px solid blue;
          opacity: 0; 
    }
    </style>
  </head>
  <body>
  <script type="text/javascript">
      function vai(){
        if (document.getElementById("div").style.opacity=="0" || document.getElementById("div").style.opacity=="") {
            document.getElementById("div").style.opacity = 1;
          }
          else {
            document.getElementById("div").style.opacity = 0;
          }
    }
    </script>
    <p onClick="vai()">aparece</p>
    <div id="div">123</div>
  </body>
</html>
 Executar

Troque a parte de seu código:

if (document.getElementById("div").style.opacity=="0") {

por

if (document.getElementById("div").style.opacity=="0" || document.getElementById("div").style.opacity=="") {

<!DOCTYPE html>
<html>
  <head>
    <title>teste</title>
    <style type="text/css">
      div{
        height: 100px;
          width: 100px;
          border: 5px solid blue;
          opacity: 0; 
    }
    </style>
  </head>
  <body>
  <script type="text/javascript">
      function vai(){
        if (document.getElementById("div").style.opacity=="0" || document.getElementById("div").style.opacity=="") {
            document.getElementById("div").style.opacity = 1;
          }
          else {
            document.getElementById("div").style.opacity = 0;
          }
    }
    </script>
    <p onClick="vai()">aparece</p>
    <div id="div">123</div>
  </body>
</html>
 Executar

Fonte Link
Enio
  • 1,5mil
  • 4
  • 20
  • 34

<!DOCTYPE html>
<html>
  <head>
    <title>teste</title>
    <style type="text/css">
      div{
        height: 100px;
          width: 100px;
          border: 5px solid blue;
          opacity: 0; 
    }
    </style>
  </head>
  <body>
  <script type="text/javascript">
      function vai(){
        if (document.getElementById("div").style.opacity=="0" || document.getElementById("div").style.opacity=="") {
            document.getElementById("div").style.opacity = 1;
          }
          else {
            document.getElementById("div").style.opacity = 0;
          }
    }
    </script>
    <p onClick="vai()">aparece</p>
    <div id="div">123</div>
  </body>
</html>
 Executar