Javascript显示隐藏 - 将复选框中的切换操作更改为<a href=""> links</a>

时间:2012-09-05 05:22:32

标签: javascript

目前我有一种基于表单复选框字段显示/隐藏div的方法,如下所示。但我想要的是不要使用表单来显示隐藏,而只是根据链接上的简单来调用show / hide函数。我希望这是有道理的,我正在尝试做什么。任何帮助/建议都会非常有价值!

<!-- Show hide-->
<script language="JavaScript">
function showhidefield()
{
if (document.goform.areas.checked)
{
document.getElementById("areaone").style.display = "block";
document.getElementById("areatwo").style.display = "none";
}
else
{
document.getElementById("areaone").style.display = "none";
document.getElementById("areatwo").style.display = "block";

}
}
</script>

<form name="goform" id="goform" action="xxxx" method="post" enctype="multipart/form-data">

<label><input name="areas" type="checkbox" onclick="showhidefield()" value="1"> Yes </label>

</form>

<div id="areaone" style="display:none;">
Area One
</div><!-- / Hideable area -->


<div id="areatwo" style="display:block;">
Area two
</div>

更改上述内容,以便不使用表单复选框来显示隐藏,而是根据事件进行切换效果,例如

<a href="xxx">Show Areaone / Hide Areatwo</a>

<a href="xxx">Show Areatwo / Hide Areaone</a>

1 个答案:

答案 0 :(得分:1)

一般方法

一般方法是使用链接标记的onclick属性。您可以直接在标签上设置,如下所示:

<a onclick="showhidefield()" href="javascript:void(0);">Show/Hide</a>

示例1

这是一个完整的工作示例:

<!DOCTYPE html>
<html>
  <head></head>
  <body>
    <div id="areaone" style="display:none;">
      Area one
    </div>
    <div id="areatwo" style="display:block;">
      Area two
    </div>
    <script type='text/javascript'>
      function showOneHideTwo(){
          document.getElementById("areaone").style.display = "block";
          document.getElementById("areatwo").style.display = "none";
      }

      function showTwoHideOne(){
          document.getElementById("areaone").style.display = "none";
          document.getElementById("areatwo").style.display = "block";
      }      
    </script>
    <a onclick="showOneHideTwo()" href="javascript:void(0);">Show one / Hide two</a>
    <a onclick="showTwoHideOne()" href="javascript:void(0);">Show two / Hide one</a>
  </body>
</html>

示例2(更好!)

但是,对于variety of reasons,如果稍微不那么直观,最好使用javascript设置onclick属性,而不是直接将其添加到html中。这是一个更好的完整工作示例:

<!DOCTYPE html>
<html>
  <head></head>
  <body>
    <div id="areaone" style="display:none;">
      Area one
    </div>
    <div id="areatwo" style="display:block;">
      Area two
    </div>
    <a id='showOneLink' href=''>Show one / Hide two</a>
    <a id='showTwoLink' href=''>Show two / Hide one</a>
    <script type='text/javascript'> <!-- This allows for better placement of the script as well... -->
      //Same functions as before
      function showOneHideTwo(){
          document.getElementById("areaone").style.display = "block";
          document.getElementById("areatwo").style.display = "none";
      }

      function showTwoHideOne(){
          document.getElementById("areaone").style.display = "none";
          document.getElementById("areatwo").style.display = "block";
      }

      //this time, we set the onclick here
      //this is better form- it keeps the content (html) and the scripting (javascript) seperate
      document.getElementById("showOneLink").onclick = function(){showOneHideTwo(); return false;}
      document.getElementById("showTwoLink").onclick = function(){showTwoHideOne(); return false;}
    </script> 
  </body>
</html>