我想获得窗口的宽度&高度+ div的宽度&高度来找到调整的宽度。我也试图绝对定位div与jquery,但我的代码不起作用。我将提供下面的代码。jquery css选择器不工作
CSS:
#inner {
width: 500px; height: 300px;
background: #ff0000;
}
的jQuery:
$(document).ready(function() {
var screenWidth = $(window).width();
var screenHeight = $(window).height();
var boxWidth = $("#inner").width();
var boxHeight = $("#inner").height();
var adjustedWidth = screenWidth - boxWidth;
var adjustedHeight = screenHeight - boxHeight;
$("#inner").css('position', 'absolute');
$("#inner").css('top', adjustedWidth + 'px');
$("#inner").css('left', adjustedHeight + 'px');
});
我在做什么错在我的jQuery?希望我的解释清楚。
“我的代码不工作”是什么意思?你有错误吗?你没有得到你期望的结果吗?你得到了什么,你期望什么?请创建一个http://jsfiddle.net/演示。 –
您是否可以发布关联的HTML以防它在问题中起作用? –
试过一个jsfiddle:http://jsfiddle.net/gnvaG/它似乎工作正常(检查结果的来源)。你可能没有正确包含jQuery,或者你的HTML不正确。 – jgthms