指定的宽度(以百分比表示),但希望以像素为单位 [英] Assigned width in percentage but want to get it in pixels

查看:110
本文介绍了指定的宽度(以百分比表示),但希望以像素为单位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.test {border:1px solid; cursor:pointer; height:10px;位置:相对; width:100%;}

 < div id = test1class =test>< / div>  



我已经显示我的HTML id 及其CSS。现在当我做 $('#test')。width()我得到 100 。我希望其 width 以像素为单位(不在)。



任何人都可以告诉如何获取其宽度的像素。

解决方案

.width / code>根据jQuery width文档获取...当前计算宽度的元素: http://api.jquery.com/width/ ,因此 $('#heatMapBar')的返回值width()是以像素,而不是百分比。我建议使用开发工具来检查宽度,它可能是在 #heatMapBar 的当前上下文,其宽度为100px。



如果你看这里: http://jsfiddle.net/NkQXa/1/你会看到 #test 设置为 width:50%; ,但它警告实际像素宽度。


.test {
  border: 1px solid;
  cursor: pointer;
  height: 10px;
  position: relative;
  width: 100%;
}

<div id="test1" class="test"></div>

I have displayed my HTML id and its CSS. Now when I am doing $('#test').width() I am getting 100. I want its width in pixels (not in %).

Can anyone tell how to get its width in pixels?

解决方案

.width() gets "...the current computed width" of the element that is used on, per the jQuery width documentation: http://api.jquery.com/width/, so the return value from $('#heatMapBar').width() is in pixels, not percent. I would suggest using developers tool to check the width, it may be that in #heatMapBar's current context, its width is 100px.

If you look here: http://jsfiddle.net/NkQXa/1/ you will see that #test is set to width:50%;, but it alerts the actual pixel width.

这篇关于指定的宽度(以百分比表示),但希望以像素为单位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆