我有这个HTML:

<div class="columna1"> CONTENT1 </div>
<div class="columna2"> CONTENT2 </div>


而这个JavaScript:

column1_height = $('#columna1').height();
column1_width = $('#columna1').width();
column2_height = $('#columna2').height();
column2_width = $('#columna2').width();


但是,当我调整屏幕大小时,值不会更新。

谢谢

最佳答案

尝试这个:

$(document).on('ready', initFunction);
$(window).on('resize', getSizes);

var column1_height = 0;
var column1_width = 0;
var column2_height = 0;
var column2_width = 0;

function initFunction(){
    getSizes();
}

function getSizes(){
    column1_height = $('#columna1').height();
    column1_width = $('#columna1').width();
    column2_height = $('#columna2').height();
    column2_width = $('#columna2').width();

    console.log("Column 1: (Height: " +column1_height+ ") - (Width: " +column1_width+ ")");
    console.log("Column 2: (Height: " +column2_height+ ") - (Width: " +column2_width+ ")");
}


这应该工作

关于javascript - 获取列的高度和宽度并更新大小,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36068810/

10-09 16:20