从顶部改变div的高度

时间:2015-04-03 10:21:04

标签: jquery html css

通过使用jquery,我想从顶部切割一个方形高度。似乎默认它从底部开始。我怎么能改变它?

这是一个小提琴:http://jsfiddle.net/b4wcspy1/

<style>
.square {
height:100px;
width: 100px;
background:black;
position:absolute;

}
</style>
<body>
<div class="square"></div>
<script>
$('div').click(function(){
$(this).css('height','60px')})
</script>
</body>
</html>

3 个答案:

答案 0 :(得分:2)

这是js:

 $('div').click(function(){
         $init = $(this).height(); 
         $slice = 60;
        $(this).css({'height':$slice+'px',
                     'margin-top':$init-$slice +'px'
                    })

    })

https://jsfiddle.net/9n29ex0o/1/

答案 1 :(得分:0)

我已经这样做了,请在js

中使用以下代码
$('div').click(function(){
            $(this).css({"height":"60px","margin-top":"40px"})
});

答案 2 :(得分:0)

试试这个:

<style>
.square {
height:100px;
width: 100px;
background:black;
position:absolute;

}
</style>
<body>
<div class="square"></div>
<script>
$('div').click(function(){
$(this).height("60");})
</script>
</body>
</html>



**$(this).height("60");**

这是jQuery的功能。 您可以从here

获取更多信息