1

我发现 jquery attr() 方法不喜欢接受带有“px”的值。生成的图像最终的宽度和高度为零!这是错误、疏忽还是某些功能?

这很容易解决,但我真的不喜欢设置没有单位的值。它可能导致不可预测的行为。

在 Firefox 3.6 和 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.

});
4

1 回答 1

5

当您使用 时attr,您正在设置一个属性。您css用于设置style属性。

您不会在 HTMLpx中的width属性中使用,因此您也不会在其中使用它attr

于 2011-02-14T14:00:12.477 回答