【问题标题】:After page load, button only works when it is clicked the second time页面加载后,按钮仅在第二次单击时有效
【发布时间】:2014-02-08 02:40:02
【问题描述】:

我正在尝试创建在单击时显示其他 div 的按钮(即 div)。例如,Button01 显示 Div01,Button02 显示 Div02。一次只能显示一个 Div。我有这部分工作,但我遇到了一个问题,在页面加载后,单击功能在单击一次按钮 div 之前不起作用(这似乎什么都不做)。在第一次单击之后,按钮 div 一切正常。为什么会发生这种情况?如何才能使点击功能立即起作用?

HTML

<div id="showOne" class="button" onClick="showOne()">Show One</div>
<div id="showTwo" class="button" onClick="showTwo()">Show Two</div>
<div id="One"></div>
<div id="Two"></div>

CSS

.button {
    width:70px;
    height:81px;
    background-color:#ccc;  
    float:left;
    text-align:center;
}

#One {
    width:70px;
    height:81px;
    background-color:blue;  
    float:left;
    display:none;
}

#Two {
    width:70px;
    height:81px;
    background-color:red;   
    float:left;
    display:none;
}

Javascript

function showOne(){
    if (document.getElementById('Two').style.display != "none") {
        document.getElementById('Two').style.display = "none";
    };

    if (document.getElementById('One').style.display == "none") {
        document.getElementById('One').style.display = "block";
    } else {
        document.getElementById('One').style.display = "none";
    };
};

function showTwo(){
    if (document.getElementById('One').style.display != "none") {
        document.getElementById('One').style.display = "none";
    };

    if (document.getElementById('Two').style.display == "none") {
        document.getElementById('Two').style.display = "block";
    } else {
        document.getElementById('Two').style.display = "none";
    };
};

演示: http://jsfiddle.net/7BtZn/1/

如您所见,使用“无换行”,按钮仅在第一次单击按钮后才起作用(这是在我的实际页面上发生的情况)。值得注意的是,使用“onLoad”,按钮根本不起作用。我做错了什么?

【问题讨论】:

    标签: javascript css html


    【解决方案1】:

    这是因为.style.display 仅在当前直接在元素上设置时为您提供设置。它不会为您提供样式表的结果。

    要修复它,请将与默认(样式表)值的比较与"" 进行比较而不是"none",然后将其设置为"" 而不是"none" 以返回默认值。

    演示: http://jsfiddle.net/7BtZn/5/

    function showOne(){
        var one = document.getElementById('One');
        var two = document.getElementById('Two');
    
        if (two.style.display != "") {
            two.style.display = "";
        }
    
        if (one.style.display == "") {
            one.style.display = "block";
        } else {
            one.style.display = "";
        }
    }
    
    function showTwo(){
        var one = document.getElementById('One');
        var two = document.getElementById('Two');
    
        if (one.style.display != "") {
            one.style.display = "";
        }
    
        if (two.style.display == "") {
            two.style.display = "block";
        } else {
            two.style.display = "";
        }
    }
    

    我还通过将元素保存到变量来减少代码。这样会更干净,更不容易出错。

    我也删除了一堆不必要的分号。


    这是您的代码的更 DRY 版本:

    演示: http://jsfiddle.net/7BtZn/6/

    function swapShow(el1, el2) {
        if (el1.style.display != "") {
            el1.style.display = "";
        }
    
        if (el2.style.display == "") {
            el2.style.display = "block";
        } else {
            el2.style.display = "";
        }
    }
    
    function showOne(){
        swapShow(document.getElementById('Two'),
                 document.getElementById('One'));
    }
    
    function showTwo(){
        swapShow(document.getElementById('One'),
                 document.getElementById('Two'));
    }
    

    【讨论】:

    • 谢谢!完美运行:D
    【解决方案2】:

    这是因为style 对象没有值。将您的代码更改为:

    <div id="One" style="display: none;"></div>
    <div id="Two" style="display: none;"></div>
    

    或者 display: block; 如果您需要它。

    理想情况下,您应该避免使用内联 onclick 事件处理程序并使用 JavaScript 切换类,而不是滥用样式对象 - 这绝不是可扩展的。

    【讨论】:

      【解决方案3】:

      JSFIDDLE

      window.onload = function(){
          var hide = function( el ){ el.style.display = 'none'; },
              show = function( el ){ el.style.display = 'block'; },
              one  = document.getElementById( 'One' ),
              two  = document.getElementById( 'Two' );
      
          document.getElementById( 'showOne' )
              .onclick = function(){ show( one ); hide( two ); };
          document.getElementById( 'showTwo' )
              .onclick = function(){ show( two ); hide( one ); };
      };
      

      或者:

      JSFIDDLE

      HTML

      <div id="showOne" class="square button">Show One</div>
      <div id="showTwo" class="square button">Show Two</div>
      <div id="One" class="square hidden"></div>
      <div id="Two" class="square hidden"></div>
      

      CSS

      .button {
          background-color:#ccc;  
          text-align:center;
      }
      
      .square {
          width:70px;
          height:81px;
          float: left;
      }
      #One {
          background-color:blue;  
      }
      #Two {
          background-color:red;   
      }
      
      .hidden {
          display:none;
      }
      

      JavaScript

      window.onload = function(){
          var hide = function( el ){ el.className += ' hidden'; },
              show = function( el ){ el.className = el.className.replace( /\s*\bhidden\b/g, '' ); },
              one  = document.getElementById( 'One' ),
              two  = document.getElementById( 'Two' );
      
          document.getElementById( 'showOne' )
              .onclick = function(){ show( one ); hide( two ); };
          document.getElementById( 'showTwo' )
              .onclick = function(){ show( two ); hide( one ); };
      };
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-25
        • 1970-01-01
        • 2019-02-17
        • 1970-01-01
        相关资源
        最近更新 更多