如何通过JQuery修改div元素的CSS属性(高度宽度)?

cor*_*ore 2 css jquery

为什么这不能在窗户的骑行侧创建一个垂直的5px宽的酒吧?

<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>
Run Code Online (Sandbox Code Playgroud)

Ran*_*ell 6

请改用:

我发现了几个问题:

  • 而不是$(window).height,使用$(window).height()
  • 而不是left: $(window).width - 5,使用right: 0

    $(document).ready(function() {
        $("#scrollBar").css({
            height: $(window).height(),
            width: 5,
            position: "absolute",                
            top: 0,
            right: 0,
            margin: 0
        });
    });
    
    Run Code Online (Sandbox Code Playgroud)