Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery-计算元素的宽度_Jquery_Width_Element_Calculated Columns - Fatal编程技术网

jQuery-计算元素的宽度

jQuery-计算元素的宽度,jquery,width,element,calculated-columns,Jquery,Width,Element,Calculated Columns,我想通过用导航的宽度减去窗口的宽度来计算元素的宽度(#content)。事实上,我不知道我的代码失败了。如果有人能帮我,那就太好了 $(function () { $(window).resize(function () { var widthWindow = $(window).width(); var widthNav = $("nav").width(); $("#content").css({ "width": widthWindo

我想通过用导航的宽度减去窗口的宽度来计算元素的宽度(#content)。事实上,我不知道我的代码失败了。如果有人能帮我,那就太好了

$(function () {
    $(window).resize(function () {
        var widthWindow = $(window).width();
        var widthNav = $("nav").width();
        $("#content").css({ "width": widthWindow - widthNav });

        // Put the console log here
        console.log(widthWindow, widthNav);
    }).resize();
}); 

您没有调用包装函数。另外,您正在自己调用
.resize()
函数

(function() {    
    $(window).resize(function () {
    var widthWindow = $(window).width();
    var widthNav = $("nav").width();
    $("#content").width(widthWindow - widthNav); // as suggested by @palash
    });
 })();

如果您最后将
console.log(widthWindow,widthNav)
放入resize事件中,您会得到什么?
ReferenceError:widthWindow未定义
您是否将其放置在与我放置在您的代码中相同的位置?现在,是的。我得到了这个
1262320
-似乎有用!你的代码到底做什么?谢谢您可以尝试这样做:
$(“#content”).width(widthWindow-widthNav)