2013-12-09 42 views
0

我需要在英文measure中显示一个包含维度和权重的DIV,并且有一个链接允许用户切换到隐藏的DIV,而不是显示度量。尝试下面的标记,但它不起作用。它拒绝切换。我已经用第一个div给出了没有样式和样式“”显示:块;“的测试。并且都不起作用。我错过了什么?需要切换2 DIV的DIV可见性

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
    <meta charset="utf-8"> 

    <style type="text/css"> 
    <!-- 
    body { 
     color:#000000; 
     background-color:#FFFFFF; 
    } 
    a { color:#0000FF; } 
    a:visited { color:#800080; } 
    a:hover { color:#008000; } 
    a:active { color:#FF0000; } 
    --> 
    </style> 
    <!--[if IE]> 
    <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script> 
    <![endif]--> 

    <script type="text/javascript"> 
    function toggle_visibility(eng, met) { 
     var e1 = document.getElementById(eng); 
     var e2 = document.getElementById(met); 
     if(e1.style.display == 'block') 
      e1.style.display = 'none'; 
      e2.style.display = 'block'; 
     else 
      e1.style.display = 'block'; 
      e2.style.display = 'none'; 
    } 
    </script> 
    </head> 
    <body> 
    <div id="eng" style="display: block;"> 
     This is English<br /> 
     <a href="#" onclick="toggle_visibility('eng','met');">Convert to Metric</a> 
    </div> 
    <div id="met" style="display: none;"> 
     This is Metric<br /> 
     <a href="#" onclick="toggle_visibility('met','eng');">Convert to English</a> 
    </div> 
    </body> 
</html> 
+0

是的,那些如果声明需要大括号。有人在Visual Basic中编码? ;) – RealityDysfunction

回答

2

试试这个if语句:

if(e1.style.display == 'block') { 
     e1.style.display = 'none'; 
     e2.style.display = 'block'; 
    } else { 
     e1.style.display = 'block'; 
     e2.style.display = 'none'; 
    } 
+0

没有。仍然保持冻结在最初的DIV。 –

+0

不,它的工作,看看这里:http://jsfiddle.net/NTN6f/ –

0

得到它的工作。我想我会发布解决方案:

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
    <meta charset="utf-8"> 

    <style type="text/css"> 
    <!-- 
    body { 
     color:#000000; 
     background-color:#FFFFFF; 
    } 
    a { color:#0000FF; } 
    a:visited { color:#800080; } 
    a:hover { color:#008000; } 
    a:active { color:#FF0000; } 
    --> 
    </style> 
    <!--[if IE]> 
    <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script> 
    <![endif]--> 

    <script type="text/javascript"> 
    function toggle_visibility() { 
     var e1 = document.getElementById("eng"); 
     var e2 = document.getElementById("met"); 
     if(e1.style.display == 'block') { 
     e1.style.display = 'none'; 
     e2.style.display = 'block'; 
     } 
     else { 
     e1.style.display = 'block'; 
     e2.style.display = 'none'; 
     } 
    } 
    </script> 
    </head> 
    <body> 
    <div id="eng" style="display: block;"> 
     This is English<br /> 
     <a href="#" onclick="toggle_visibility();">Convert to Metric</a> 
    </div> 
    <div id="met" style="display: none;"> 
     This is Metric<br /> 
     <a href="#" onclick="toggle_visibility();">Convert to English</a> 
    </div> 
    </body> 
</html>