Home  >  Article  >  Web Front-end  >  js不能获取隐藏的div的宽度只能先显示后获取_javascript技巧

js不能获取隐藏的div的宽度只能先显示后获取_javascript技巧

WBOY
WBOYOriginal
2016-05-16 16:37:331407browse
var tipsWidth = $(".Loading").css("width").replace("px", "") //先获取 
var windowWidth = $(window).width(); 
$(".Loading").css({ "display": "block" }); //再显示 
$(".Loading").css({ "left": (windowWidth - tipsWidth) / 2 + "px" }); 

我要获取一个根据内容自适应宽度的div的实际宽度,这个div初始是隐藏的,我要获取它的实际宽度,对他进行居中操作,弄了很长时间,总是获取不到该div的真实宽度,后来发现js不能获取隐藏div的的宽度,现在的办法是先将其显示出来,再获取其宽度,计算位置

$(".Loading").css({ "display": "block" });// 先显示 
var tipsWidth = $(".Loading").css("width").replace("px", "") //再获取 
var windowWidth = $(window).width(); 
$(".Loading").css({ "left": (windowWidth - tipsWidth) / 2 + "px" });
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn