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