用javascript添加点击高度 [英] Add height on click with javascript
本文介绍了用javascript添加点击高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我很难弄清楚如何为我的 div
添加高度。考虑到这个Html:
< div id ='box'>< / div>
< div id ='plus'onClick ='resize()'> add height< / div>
我正在使用这个方法:
- 点击我读取div的当前高度
- 在此之后,我将字符串转换为整数
- 然后我将一些像素添加到盒子高度。
JS看起来像这样:
function resize(){
//获取元素
var height = document。的getElementById( '盒子')style.height。
//将元素从字符串转换为整数
height = height.replace(px,);
height = + height;
//改变高度
var n = height;
height = n + 300 +'px';
$ / code $
这应该是非常基本的,但我无法弄清楚我是什么失踪。
解决方案请参阅 示例小提琴
您错过了:
document.getElementById('box')。style.height = height
I am having a hard time figuring out how to add height to my div
. Considering this Html :
<div id='box'></div>
<div id='plus' onClick='resize()'>add height</div>
I am using this method:
- on click i read the current height of the div
- after that i convert the string i get to integer
- then i add some pixels to the box height
The JS looks like this:
function resize() {
//get element
var height=document.getElementById('box').style.height;
//transform element from string to integer
height=height.replace("px","");
height=+height;
//change height
var n=height;
height=n+300+'px';
}
This should be pretty basic but i can't figure out what i'm missing.
解决方案 See this : Sample fiddle
You missed:
document.getElementById('box').style.height = height
这篇关于用javascript添加点击高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文