如何解决使用 JQuery 设置框的高度
我想改变一个id为#block2的盒子的高度,我会根据用户的浏览器窗口来改变它,如果窗口长度是1000px那么盒子高度就是1000px。
每次调整浏览器窗口大小时,我都会检查它的大小
var winHeight = $(window).height(),winWidth = $(window).width();
$(window).resize(function(){
$('#block2').css("height",winWidth + "px");
console.log($('#block2').height())
});
如果有问题请更正..因为我写的代码在浏览器中不起作用
解决方法
您可以通过以下方式使用 jquery 获取元素的高度:
$('#yourelementid').css('height');
还要在 resize
事件中计算窗口宽度、高度,然后尝试应用。
查看此演示:
$(document).ready(function() {
$(window).resize(function() {
var winHeight = $(window).height();
var winWidth = $(window).width();
$('#block2').css("height",winWidth + "px");
alert($('#block2').css('height'));
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="block2" style"border:1px solid black">
<p>content</p>
</div>
var $win = $(window);
您可以使用
获取元素的高度和宽度$win.height()
和 $win.width()
您可以通过
设置元素的高度 $("#yourEelementID").height('Your value');
就您而言,
$win.on('resize',function(){
$("#block2").height($win.width());
console.log($('#block2').height());});
使用,检查当前元素高度,
console.log($('#block2').height());
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。