如何通过JQuery修改div元素的CSS属性(高度-宽度)?
为什么这不会在车窗的骑乘侧创建一个5px宽的垂直杆如何通过JQuery修改div元素的CSS属性(高度-宽度)?,jquery,css,Jquery,Css,为什么这不会在车窗的骑乘侧创建一个5px宽的垂直杆 <html> <head runat="server"> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript"> $(document).ready(function() { $("#scrollBar").
<html>
<head runat="server">
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#scrollBar").css({
height: $(window).height,
width: 5,
position: "absolute",
top: 0,
left: $(window).width - 5,
margin: 0
});
});
</script>
</head>
<body>
<div id="scrollBar" style="background-color: Red">test
</div>
</body>
</html>
$(文档).ready(函数(){
$(“#滚动条”).css({
高度:$(窗口)。高度,
宽度:5,
位置:“绝对”,
排名:0,
左:$(窗口)。宽度-5,
保证金:0
});
});
测试
您可以尝试右:0
,而不是左:$(窗口).width-5
编辑:是的,那太愚蠢了。但是,如果我没有弄错的话,这些也应该被引用,比如在“宽度”中:“5px”
$("#scrollBar").css({
'height': $(window).height(),
'width': '5px',
'position': 'absolute',
'top': 0,
'right': 0,
'margin': 0
});
还要注意jQuery的.width和.height应该是.width()和.height() 您可能希望将宽度设置为“5px”,而不是5,将高度设置为$(窗口)。高度()+“px”,将左侧设置为($(窗口)。宽度()-5)+“px”。请使用此选项: 我发现了几个问题:
- 使用
$(窗口).height()代替
$(窗口).height()
- 使用
right:0来代替
left:$(窗口).width-5
$(document).ready(function() { $("#scrollBar").css({ height: $(window).height(), width: 5, position: "absolute", top: 0, right: 0, margin: 0 }); });
PS:Jonathas是正确的高度和宽度是函数,而不是属性。正确的唯一问题是IE经常弄脏它,并且是1px。填充/边距会有帮助吗?我现在没有IE,所以无法测试。