如何确定宽度是否有px或% [英] How to determine if width has px or %

查看:170
本文介绍了如何确定宽度是否有px或%的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何判断css属性,如width ex: width:100%是否有px或%分配给它。
in chrome例如如果我有 width:250px; 和我做 $('div')。width()我得到 250 ,而如果我使用百分比,我只是得到的宽度的px的百分比,基于我的屏幕分辨率。

How can i tell if a css property such as width ex: width:100% has px or % assigned to it. in chrome for example if i have width:250px; and i do $('div').width() i get 250 whereas if i use percentage, i just get the width in px for the percentage based on my screen resolution.

推荐答案

这应该可以工作。

var value = $('#id').get(0).style.width;
var hasPx = value.indexOf('px') >= 0;
var hasPct = value.indexOf('%') >= 0;

JSBin: http://jsbin.com/asewit/2/edit#javascript,html

这篇关于如何确定宽度是否有px或%的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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