Javascript 重新居中调整窗口大小时的div会得到“我”;“未捕获的类型错误”;
我已经在我的页面上设置了一个模式窗口,它工作得很好,但需要在调整窗口大小时重新定位以保持居中。不幸的是,我只是得到了一个错误: 未捕获的TypeError:对象#没有方法“outerHeight” 我是否用错了“this”Javascript 重新居中调整窗口大小时的div会得到“我”;“未捕获的类型错误”;,javascript,jquery,Javascript,Jquery,我已经在我的页面上设置了一个模式窗口,它工作得很好,但需要在调整窗口大小时重新定位以保持居中。不幸的是,我只是得到了一个错误: 未捕获的TypeError:对象#没有方法“outerHeight” 我是否用错了“this” $(window).resize(function() { //If mask is open, resize modal elements if ($('#modalMask').is(':visible'))
$(window).resize(function() {
//If mask is open, resize modal elements
if ($('#modalMask').is(':visible')) {
//Resize and position modal windows
//Get window dimensions
var mWinHeight = $(window).height();
var mWinWidth = $(window).width();
//Make sure all open modal windows are centred
$('.modalWindow:visible').each(function (i) {
var $this = $(this);
$this.css('top', mWinHeight/2-(this.outerHeight()/2));
$this.css('left', mWinWidth/2-(this.outerWidth()/2));
});
}
});
忘了一些“$”:
$this.css('top', mWinHeight/2-($this.outerHeight()/2));
$this.css('left', mWinWidth/2-($this.outerWidth()/2));
我不知道
outerHeight
是否是domeElement的属性,但您必须删除括号才能使用javascript属性:
this.outerHeight
或者从中创建一个jquery对象,以使用.outerHeight()jquery方法:
$(this).outerHeight()
// or use $this.outerHeight()
每个人都会这样。不用担心。谢谢,@sje397在你之前的一瞬间就搞定了——我少了一美元。是的。重要的是你解决了你的问题:o)追加投票仍然是一个选项,顺便说一句;-)