简体   繁体   English

如何在单击按钮时使div的高度更大,然后再次单击时又变回其原始高度?

[英]How can I make the height of a div bigger onclick of a button and then change back to it's original height when click again?

I have a div id="coding" set on height:300px on CSS . 我在CSS上将height:300px设置为div id="coding" when I click another div id="menu" , I want #coding to change it's height to 800px . 当我单击另一个div id="menu" ,我希望#coding将其高度更改为800px I managed to do that like this 我设法做到了

<script>
    function changec() {
        document.getElementById('coding').style.height = "800px";
    }
</script> 


Now, when click the #menu again, I want the height to get back to it's original 300px value. 现在,再次单击#menu时,我希望高度恢复到其原始的300px值。 Can someone help? 有人可以帮忙吗? The code is: 代码是:

HTML HTML

<div id="coding">
<div id="menu" onclick="changec()">≡</div>
...
</div>

CSS CSS

#coding{
    ...
    height:300px;
}

Simple check if the value is set - remove it (then CSS height will take over). 简单检查值是否已设置-删除它(然后CSS高度将接管)。

function changec() {
       var xDiv = document.getElementById('coding');

       if (xDiv.style.height == '') 
           xDiv.style.height = '800px'
       else 
           xDiv.style.height = ''
}

Demo: http://jsfiddle.net/ygalanter/BLE6N/ 演示: http//jsfiddle.net/ygalanter/BLE6N/

one of the solution for your problem is as follows: First count how many times you click on #menu now depending on your expectation you can change the javascript as follows 您的问题的解决方案之一如下:首先,根据您的期望,计算您现在单击#menu的次数,您可以如下更改javascript

<script type="text/javascript">
    var count = 0;
    function changec() {
        count++;
        if(count%2==1)
            document.getElementById("coding").style.height = "800px";
        else
            document.getElementById("coding").style.height = "300px";
    }
</script>

Another alternative solution is 另一个替代解决方案是

<script type="text/javascript">
    function changec() {
        var currentheight = document.getElementById('coding').clientHeight;
        if (currentheight == 300)
            document.getElementById('coding').style.height = "800px";
        else if (currentheight == 800)
            document.getElementById('coding').style.height = "300px";
    }
</script>

Not sure why you tagged jQuery since you didn't use it, but still...Considering the possibility that you are willing to use/learn it, I created a jsFiddle for it: http://jsfiddle.net/Tm2Hd/ . 不知道为什么不使用jQuery就标记了它,但是仍然...考虑到您愿意使用/学习它的可能性,我为此创建了一个jsFiddle: http : //jsfiddle.net/Tm2Hd/

CSS : CSS

#coding{
    border:1px solid black; /*optional: Keep track of your div's expand*/
    height:300px;
    }
#coding.larger{
    height:800px;
    }

JS : JS

 function changeHeight() {
    if($('#coding.larger').length>0)
    {
       $('#coding').removeClass("larger");
    }
    else
    {
       $('#coding').addClass("larger");
    }
}

HTML HTML

<div id="coding">
    <!--<div onclick="changeHeight()">≡</div> 
       Personally, I don't suggest using divs as clickable objects... Why don't you use buttons instead?
  -->
   <button onclick="changeHeight()">≡</button> 
    ...
</div>

My solution to your problem is: Create a new class named larger , pointing to your div, and toggle between this and the original whenever you click the button. 我对你的问题的解决方案是:创建一个名为较大的新的类,指向你的DIV,而这,只要你单击该按钮原之间切换。

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

相关问题 单击按钮时增加div的高度,再次单击同一按钮时将其减小 - increase height of div on button click, and decrease it when I click same button again 单击按钮时如何更改按钮的颜色并在再次单击按钮时将其更改回原始颜色? - How to change a button's color when clicked on it and change it back to its original color when the button is clicked again? 按钮单击更改div高度 - Change div height on button click 单击“更多”按钮时如何将div height属性更改为auto - how to change the div height property to auto when I click on '“more” button 如何在单击时更改按钮的 state 并在 React 中再次单击时恢复为原始 state? - How to change state of button on click and revert back to the original state when clicked again in React? 如何使 Div 的高度变化更慢 - How to make a Div's height change slower 我该如何固定一个div(其高度大于窗口),使其固定,但仍然可以滚动而没有任何侧边栏? - How can I make a div, which is bigger (in height) then the window, be fixed but still scroll able without any sidebar? 如何动态更改div的高度? - How can I change a div's height dynamically? 如何使div隐藏onclick并在再次单击时显示它? - How do I make a div hide onclick and make it show when it's clicked again? 使用JS单击时将div高度更改为子div的高度 - Change div height to child div's height on click with JS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM