我有一个div,我想知道是否有办法使用jQuery获得百分比的宽度.
这是我的代码:
$(document).ready(function(){
var f = $('.f').width();
alert(f);
});
Run Code Online (Sandbox Code Playgroud)
我尝试使用%这样的参数width(%),但这是将宽度设置为%不以百分比格式获取值.有人可以告诉我,我应该怎样做才能获得百分比?
任何帮助都非常感谢.
Arc*_*her 38
要使用您的示例代码......
$(document).ready(function(){
var f = $(".f").width() / $('.f').parent().width() * 100;
alert(f);
});
Run Code Online (Sandbox Code Playgroud)
如果你想要相对于身体的宽度:
var percent = $('.f').width() / $('body').width() * 100;
console.log(Math.round(percent).toFixed(2));
Run Code Online (Sandbox Code Playgroud)
这是你要的吗?
| 归档时间: |
|
| 查看次数: |
38426 次 |
| 最近记录: |