将带有"px"的值插入jquery的attr方法时,图像属性设置为零

sha*_*fty 1 jquery attributes attr

我发现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.

});
Run Code Online (Sandbox Code Playgroud)

lon*_*day 5

使用时attr,您正在设置属性.您css用于设置style属性.

您不会在HTML px中的width属性中使用,因此您也不会在HTML中使用它attr.