开发者

Get button's width attribute value with jQuery in IE

开发者 https://www.devze.com 2022-12-20 00:21 出处:网络
Having this button on my page: <input type=\"button\" id=\"Button1\" value=\"OK\" class=\"buttonClass\" width=\"150px\"/>

Having this button on my page:

<input type="button" id="Button1" value="OK" class="buttonClass" width="150px"/>

and using jQuery 1.3.2, if I run this command:

alert($('.buttonClass').attr('width'));

I get '150px' in all browsers but in IE (any version) I get '0'.

if I add a custon attribute in the button's tag, something like this:

<input type="button" id="Button1" value="OK" class="buttonClass" 开发者_运维知识库myattribute="150px"/>

then the command:

alert($('.buttonClass').attr('myattribute')); 

works in all browsers including IE and returns '150px'.

Does anyone know what is going on here and if there is a fix?


UPDATE: Simplified and removed stuff about using DOM properties rather than attr().

Firstly, the width and height attributes of an input should be integers rather than length values; pixels are assumed.

Secondly, and this is what's causing the problem (as you can prove by changing the input type to "image"), width and height attributes are only appropriate and only affect rendering in the browser for inputs of type image. For buttons, you need to use CSS to change the dimensions.


Can you guarantee that .buttonClass is unique?

If you can then you're better selecting by ID anyway

0

精彩评论

暂无评论...
验证码 换一张
取 消