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