开发者

Image attributes get set to zero when inserting values with "px" into jquery's attr method

I found that the jquery attr() method doesn't like accepting values with "px" in them. The resulting images end up with zero width and height! Is this a bug, an oversight or some feature?

It is easy to get around, but I really don't like setting values without units. It could lead to unpredictable behaviour.

Tested the following in firefox 3.6 and opera 11:

<html>
<head>
<script type="text/javascript" src="../jquery-1.4.min.js"></script>
<script type="text/javascript" src="return.js"></script>
</head>

<body>
<div id="links" style="width:500px; background:#000;">
  <img src="images/ref.png" width="500px" height="500px" alt="reference" />
</div>

</body>
</html>

$(document).ready(function(){

$('div#links').css({ 'height':"300px" });
$('div#links img').attr({ 'width':"100px", 'height':"100px" }); // This doesn't work!
//$('div#links img').attr({ 'width':"100", 'height':"100"开发者_开发问答 }); // This works.

});


When you use attr, you are setting an attribute. You use css for setting a style property.

You wouldn't use px in a width attribute in HTML, so you don't use it in attr either.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜