简体   繁体   English

对此javascript函数进行了细微调整?

[英]Minor tweak to this javascript function?

I have a script that shows/hides used via onClick. 我有一个脚本,显示/隐藏通过onClick使用的脚本。 I can get it to show/hide just fine, but I can't get it to show/'hide everything else'. 我可以让它显示/隐藏得很好,但是我不能让它显示/隐藏其他任何东西。 So what I get is a bunch of open containers when I really want just the one. 因此,当我只想要一个容器时,我得到的是一堆打开的容器。

Javascript: 使用Javascript:

<script>
    function showfields(fields){
        if(document.getElementById(fields).style.display=='none'){
            document.getElementById(fields).style.display='block';
        }
        else{
            document.getElementById(fields).style.display = 'none';
        }
    }
</script>

HTML: HTML:

<div id="hidden" class="steps" style="display: block;">
    <div class="section" style="margin-right: 10px;">
        <h2>Something</h2>
    </div>
    <button class="continue dropdown" id="showLink" onClick="showfields('hidden2');return false;" href="#">Continue</button>
</div>
<div id="hidden2" class="steps" style="display: block;">
    <div class="section" style="margin-right: 10px;">
        <h2>Something2</h2>
    </div>
    <button class="continue dropdown" id="showLink" onClick="showfields('hidden3');return false;" href="#">Continue</button>
</div>
<div id="hidden3" class="steps" style="display: block;">
    <div class="section" style="margin-right: 10px;">
        <h2>Something3</h2>
    </div>
    <button class="continue dropdown" id="showLink" onClick="showfields('hidden3');return false;" href="#">Continue</button>
</div>
<div id="hidden4" class="steps" style="display: block;">
    <div class="section" style="margin-right: 10px;">
        <h2>Something4</h2>
    </div>
    <button class="continue dropdown" id="showLink" onClick="showfields('hidden4');return false;" href="#">Continue</button>
</div>

Thanks 谢谢

Since you've tagged this with jQuery I assume that is an option: 既然您已经用jQuery标记了它,那么我认为这是一个选择:

function showfields(fields){
  $('.steps').hide()
  $('#' + fields).show();
}

Edit: Is this what you're trying to do: http://jsfiddle.net/Rykus0/67cjt/2/ (without jQuery) 编辑:这是您要执行的操作: http//jsfiddle.net/Rykus0/67cjt/2/ (无jQuery)

(ps - this can be done better) (ps-可以做得更好)


Your question is not very clear, but I'm guessing that you want the page to start with the everything but the first div hidden. 您的问题不是很清楚,但是我猜您希望页面以隐藏第一个div之外的所有内容开头。

In that case, what you have is fine, just set style="display:none;" 在这种情况下,您只需设置style="display:none;" on the elements you don't want to show at first (you can also use a class). 在您不想一开始就显示的元素上(也可以使用一个类)。

Also, I believe you should change the onclick call on hidden3 to be showfields('hidden4') and remove the onclick event from hidden4 此外,我相信您应该将showfields('hidden4')上的onclick调用更改为showfields('hidden4')并从hidden4中删除onclick事件

I agree with Porco, 我同意Porco,

Another option would be using a class as a flag, for example 另一个选择是使用类作为标志,例如

function showFields(fields){
     $(fields).each(function(){
         if($(this).hasClass('visible'){
              $(this).hide();
         }
         else {
              $(this).show();
         }
     }
}

Without using jquery .This piece of javascript may help you 不使用jquery。这段JavaScript可能对您有帮助

  <script>
        function showfields(fields){
          for(var i=0;i<document.body.getElementsByTagName("*").length;i++)
          { if(document.body.getElementsByTagName("*")[i].id==field)
            {
            document.body.getElementsByTagName("*")[i].style.display='block';
            }
            else{
                 document.body.getElementsByTagName("*")[i].style.display='block';

            }
        }
    </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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