1

jquery attr()メソッドは、「px」を含む値を受け入れるのが好きではないことがわかりました。結果の画像は、幅と高さがゼロになります。これはバグですか、見落としですか、それともいくつかの機能ですか?

簡単に回避できますが、単位なしで値を設定するのは本当に好きではありません。予期しない動作につながる可能性があります。

Firefox3.6とOpera11で以下をテストしました。

<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.

});
4

1 に答える 1

5

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.

于 2011-02-14T14:00:12.477 に答える