2012-05-17 185 views
1
jQuery(window).load(function() { 
    jQuery.each(jQuery(".extraimgs"), function() { 
     //this.height = 0 and this.width=0 in IE only 
     if (this.height > this.width) { 
      if (this.height > 263) { 
       this.height = 263; 
      } 
      if (this.width > 354) { 
       this.width = 354; 
      } 
     } 
     else { 
      this.height = 263; 
      this.width = 354; 
     } 
    }); 
}); 

HTML代码页面加载

<asp:DataList runat="server" ID="dlImages" RepeatColumns="2" RepeatDirection="Horizontal" RepeatLayout="Table" OnItemDataBound="dlImages_ItemDataBound" Style="display: none;"> 
    <ItemTemplate> 
     <div> 
      <asp:Image runat="server" ID="imgPics" class="extraimgs" /> 
     </div> 
    </ItemTemplate>         
</asp:DataList> 

过程中没有的高度和在IE中的图像的宽度我有问题,这个代码在IE中。在这里,我设置了运行时图像的高度和宽度(页面加载事件 - 使用jQuery)以及使用代码隐藏的图像源。

此代码工作得很好,除了IE8所有的浏览器。在页面加载事件中,我得到了图像的高度和宽度,但我没有得到IE8中图像的高度和宽度。我在加载事件中尝试了很多以获取图像的高度和宽度,但它不起作用。

有谁知道解决方案吗?

+2

你试过用'$(本).height()'和'$(本).WIDTH()'?这应该可以。 – ubik

+0

是的..我试了它,但它不工作。我得到0(零)作为高度和宽度 – ravidev

+0

这将是很好有一个jsfiddle玩这个。您也可以尝试使用'jQuery(“。extraimgs”).load(function(){/ * ... * /})',因为图像在代码执行时尚未加载。 – ubik

回答

-1

$(document).load()$(document).ready()而不是$(window).load()

+0

事实上,我没有注意到OP正在使用'.load()'。 – ubik

+0

OP代表哪里? – 11684

+0

“楼主” – ubik

0

试试这个:

jQuery(document.body).load(function() { 
     jQuery(".extraimgs").each(function (i) { 
     if (this.height > this.width) { 
      if (this.height > 263) { 
       this.height = 263; 
      } 
      if (this.width > 354) { 
       this.width = 354; 
      } 
     } 
     else { 
      this.height = 263; 
      this.width = 354; 
     } 
     }); 
    }); 
+0

这不工作..我没有得到使用此的高度和宽度 – ravidev

1

OK,下面是应该工作,按照我上面的注释版本:

jQuery(function() { 
    jQuery(".extraimgs").load(function() { 
     var $this = $(this); 
     if ($this.height > $this.width) { 
      if ($this.height > 263) { 
       $this.height = 263; 
      } 
      if ($this.width > 354) { 
       $this.width = 354; 
      } 
     } 
     else { 
      $this.height = 263; 
      $this.width = 354; 
     } 
    }); 
});