简体   繁体   English

Javascript:通过窗口高度设置DIV的宽度

[英]Javascript: Set Width of DIV by Window Height

I want to set the width of a div to the same value as the window height. 我想将div的宽度设置为与窗口高度相同的值。

I tried something like this: 我尝试过这样的事情:

<script type="text/javascript">
$(".rt-container").style.width = $(window).height();
</script>

But it's not working. 但这不起作用。 Maybe because I'm an absolute beginner with javascript. 也许是因为我绝对是javascript初学者。

How can I achieve it (by using jquery)? 如何实现(使用jquery)?

You can change css styles like this, use the method .css() with option and value as parameters and add the "px" to value, as mentioned in comments: 您可以像这样更改css样式,使用带有optionvalue作为参数的.css()方法,并在value中添加“ px”,如注释中所述:

<script type="text/javascript">
$(".rt-container").css("width",$(window).height() +"px");
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM