2012-11-29 37 views
0

Ive得到了一个网页的两个div的,这需要从页面加载隐藏,直到当DIV是要displayed..is if语句在脚本块使然这可能以及如何完成? 我有一个jQuery的链接,但我已经试过行不通的JavaScript隐藏的div直到if语句条件选择格

<!DOCTYPE html> 
<html> 
<head> 
    <style type="text/css"> 
     body { 
      background-color: #3da9cd; 
      background:url('background.jpg'); 
      height: 50%; 
      width: 50%; 
      margin:0; 
      padding: 0; 
      overflow:auto; 
     } 
     h1 { 
      font-size: 34px; 
      margin: 0; 
      padding: 0; 
      font-family: Georgia, "Times New Roman", Times, serif; 
      font-weight: bold; 
      color: #000000; 
     .hidden { display: none; } 
     .shown {display:block;} 
} 
</style> 
<script type="text/javascript"> 


function WinLose() 
{ 
    $("#win").hide(); 
$("#lose").hide(); 
    var x=document.getElementById("demo"); 
    x=x.innerHTML=Math.floor((Math.random()*5)+1); 

    if (x==5) 
    { 
     $('#win').show() 

    } 
    else 
    { 
     $('#lose').show() 
    } 

} 

</script> 

</head> 
<body> 


<p id="demo"></p> 

<button onclick="WinLose()">Try it</button> 

<!-- Win element --> 
     <div id="win" class="hidden">Congratulations! You have won a prize. *UNIQUE CODE* Collect your prize from the Customer Services desk</div> 

     <!-- The losing image div --> 
     <div id="lose" class="hidden">Sorry, you have not won. Better luck next time</div> 

</body> 
</html> 
+0

我们可以看到一些代码,请...这样,我们知道你在想什么方法来使用,我们可以帮助从那里 – taylorjes

回答

1

你应该使用CSS display属性,像这样设置的div的可见性:

.hidden { display: none; } 

和你的div像这样:

<div class="hidden">first div</div> 
<div class="hidden">second div</div> 

,然后用你的,JS或者删除hidden类或$.show()

+0

谢谢,尝试这样做,但迄今为止它仍然显示的所有文字,我已经添加了代码到目前为止的问题 – AisRuss

+1

你似乎已经弄错了h1''''。 – carlcalderon

+0

全部因为A} THANKYOU:D – AisRuss

2

CSS:

.DIV_CLASS{ 
    display:none; 
} 

JQuery的:

$(document).ready(function(){ 
    if(some statment){ 
     $('.DIV_CLASS').show(); 
    } 
}); 
0

你要么显示的是该DIV if语句或使用setInterval()setTimeout()定期检查是否有语句集。