使用jQuery以%%获取html元素的宽度
如果我alert
css选择器设置为width:100%
的元素width:100%
我在px
得到它,是否有某种方法可以将它从css中设置为%
,我需要它来修复一些具有流畅布局的脚本。
// css .my-element { width:100%; } // javascript alert('width: '+$('.my-element').css('width')); // this alerts "width: 1116px" instead of "width: 100%"
也许这样的事情总是如此?
alert($('.my-element').outerWidth() == $('.my-element').parent().outerWidth());
function percentwidth(elem){ var pa= elem.offsetParent || elem; return ((elem.offsetWidth/pa.offsetWidth)*100).toFixed(2)+'%'; }
您可以通过两种方法获得百分比宽度。
现场演示
使用jQuery使用jQuery 1.3.2,你将得到100%。
alert($('.my-element').css('width'));
使用javascript通过在标签的样式属性中设置宽度这样
div2 = document.getElementById('div2'); alert("Width of div2 with style = " + div2.style.width);
这个post
根据开发人员用CSS指定的内容,是否可以使用jQuery以百分比或像素为单位获取元素的宽度?