开发者

jQuery: width() not working

开发者 https://www.devze.com 2023-01-10 20:05 出处:网络
here\'s my simple code: var msgbox = $(\"<div style=\'width:320px;height:200px;\'></div>\");

here's my simple code:

var msgbox = $("<div style='width:320px;height:200px;'></div>");
alert(msgbox.width());

the al开发者_Go百科ert gives me zero - what's wrong? why isn't it 320?

thx


You should insert your div in the document first. Or use .css("width") instead.


You need to first append the element to the DOM before trying to measure its size:

$('body').append(msgbox);
alert(msgbox.width());
0

精彩评论

暂无评论...
验证码 换一张
取 消