[英]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.