如何使用JavaScript获取元素的填充值? [英] How to get an element's padding value using JavaScript?

查看:89
本文介绍了如何使用JavaScript获取元素的填充值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的HTML中有一个 textarea 。我需要以像素为单位将整数或浮点数填充数值。我怎样才能使用JavaScript?我不使用jQuery,所以我正在寻找纯JavaScript解决方案。

解决方案

这将返回 padding-left value:

  window.getComputedStyle(txt,null).getPropertyValue('padding )

其中 txt 是引用您的TEXTAREA元素。



以上这些工作适用于所有现代浏览器和IE9。但是,它在IE8及以下版本中不起作用。

现场演示 http://jsfiddle.net/simevidas/yp6XX/

详细阅读: https://developer.mozilla.org/en/DOM:window.getComputedStyle






顺便说一句,这只是为了比较,这是如何使用jQuery完成相同的工作:

  $(txt).css('padding-left')

以上在IE6-8中工作。


I have a textarea in my HTML. I need to get the padding numerical value in pixels as either integer or float. How can I get it using JavaScript? I am not using jQuery, so I'm looking for pure JavaScript solutions.

解决方案

This will return the padding-left value:

window.getComputedStyle(txt, null).getPropertyValue('padding-left')

where txt is the reference to your TEXTAREA element.

The above works in all modern browsers and in IE9. However, it does not work in IE8 and below.

Live demo: http://jsfiddle.net/simevidas/yp6XX/

Further reading: https://developer.mozilla.org/en/DOM:window.getComputedStyle


Btw, just for comparison, this is how you get the same job done using jQuery:

$(txt).css('padding-left')

The above does work in IE6-8.

这篇关于如何使用JavaScript获取元素的填充值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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