通过Jquery改变Div的高度和宽度
Changing Height and Width of Div via Jquery
我试图用方形盒子填充div容器。为什么这条线不行?我似乎不能改变我的div #gridSquare
的高度和宽度。
$('#gridSquare').css({"height": "38.4", "width": "38.4"});
我将小提琴与我的其他代码连接起来。
感谢您的阅读!
小提琴
我最终想做的是使用squareSide变量来设置高度和宽度
你需要使用一个类而不是一个id (id应该是唯一的),然后你想要设置正方形的css 在之后你已经添加到正方形:
var squareSide = 960 / 25;
console.log(squareSide);
for (var rows = 0; rows < 25; rows++) {
$('<div class="gridSquare"></div>').appendTo('.container')
for (var cols = 0; cols < 25; cols++) {
$('<div class="gridSquare"></div>').appendTo('.container')
}
}
$('.container').on('mouseenter', '.gridSquare', function() {
$(this).css('background-color', 'white');
});
$('.gridSquare').css({
"height": "38.4",
"width": "38.4"
});
.container{
background-color: grey;
margin: 0 auto;
text-align: center;
font-size: 0;
margin-bottom: 30px;
width: 960px;
height: 960px;
}
.gridSquare{
background-color: black;
display: inline-block;
vertical-align: top;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container"></div>
for循环也可以改成
// 625 = 25 * 25
for (var i = 0; i < 625; i++) {
$('<div class="gridSquare"></div>').appendTo('.container')
}
可能
$('#gridSquare').css({"height": "38.4px", "width": "38.4px"});
用class
代替id
添加。
$(document).ready(function(){
var squareSide = 960/25;
console.log(squareSide);
for(var rows = 0; rows < 25; rows++){
$('<div class="gridSquare"></div>').appendTo('.container')
for(var cols = 0; cols < 25; cols++){
$('<div class="gridSquare"></div>').appendTo('.container');
$('.gridSquare').css({"height": "38.4", "width": "38.4"});
}
}
$('.container').on('mouseenter', '.gridSquare', function(){
$(this).css('background-color', 'white');
});
});
操纵http://jsfiddle.net/5ojjbwms/6/
相关文章:
- 通过jquery设置最小高度
- JQuery.animate();不以固定高度工作
- 如何使用jQuery添加另一个对象的高度作为边距
- JQuery mCustomScrollbar动态高度
- jquery插件或javascript方法自动调整文本输入(而非文本区域)(固定宽度)可变高度的大小
- 动态潜水高度jQuery.Load()不工作
- 使用jQuery可以根据窗口大小更改滑块的css高度
- 使用jquery调整文本区域的高度
- 如何设置'auto'JQuery动画中的高度
- jQuery查找最大元素的高度
- 使用Bootstrap3更改Scroll Jquery上导航栏的高度
- 获取下一个或上一个隐藏元素的高度,以设置父元素的高度.jquery.
- 使用最小高度的jQuery幻灯片切换
- 制作jQuery高度百分比动画
- jQuery在.show期间设置高度动画('幻灯片')
- jQuery动画到容器的高度
- JQuery查询滚动长度/高度
- Dreaded 100%边栏高度-jQuery修复,但另一个问题出现了
- 如何更新高度 jquery/JavaScript
- 向下滚动时降低标题的高度 - jQuery