【问题标题】:jquery image width and height not working on IEjquery图像宽度和高度在IE上不起作用
【发布时间】:2013-02-07 11:04:38
【问题描述】:
<script type="text/javascript">
     $(document).ready(function () {

         $("#aspnetForm img").load(function () {
             $(this).width(64);
             $(this).height(64);
         });
     });
</script>

 <asp:TemplateField>
                <ItemTemplate>
                <img alt="" runat="server" class="pic" style="cursor:pointer" src='<%# StripHTML(Eval("cat_img").ToString()) %>'  />
                </ItemTemplate>
                </asp:TemplateField>

它可以在 Chrome 中运行,但不能在 IE 上运行。请帮我。我的图像是数据库中的&lt;p&gt; &lt;img src=....&gt; html 标记。我清除了 striphtml 方法。

【问题讨论】:

标签: jquery asp.net explorer document-ready


【解决方案1】:

试试这个:

$(document).ready(function () {
    $("#form img").css({"height":"64px","width":"64px"});
});   

小提琴:http://jsfiddle.net/devWaleed/6eas3/4/

【讨论】:

  • 这应该也可以,但是“.each()”方法比较慢,如果可以的话应该避免
  • 或者干脆使用没有javascript的css? :)
【解决方案2】:

图像尚未在ready 事件上加载,请改用$(window).load()ready 上,仅加载了 DOM 结构,而不加载真实图像。 这就像你有img 标签但还没有文件


好的,您的问题完全不同,您正在寻找这个,我的错误 您只想迭代一些图像并将它们设置为 64x64 应该可以工作

$(document).ready(function () {
    $("#aspnetForm img").each(function () {
         $(this).width(64).height(64);
    });
});

$(document).ready(function () {
    $("#aspnetForm img").each(function () {
        $(this).css({width:64+'px',height:64+'px'});
    });
});

如果你想获得每张图片的宽度,你应该等待 img 加载

 $(window).load(function () {
     var width, height;
     $("#aspnetForm img").each(function () {
         width = $(this).width();
         height = $(this).height();
         //store the current img width or height or do whatever
     });
 });

【讨论】:

  • $(window).load() 而不是 $(document).ready() 或者如果有一些闭包逻辑,你仍然可以将它放在 $(document).ready(function(){}) 中。只需确保有关图像大小的逻辑在 $(window).load(function(){....})
  • 将 documen.ready 替换为 window.load 苦差事也不起作用..:(
【解决方案3】:

不要使用.load(),它已被弃用。

试试这个,应该适用于所有浏览器:

$(document).ready(function() {
    $('#aspnetForm img').css('width', '64px')
                        .css('height', '64px');
});

【讨论】:

  • 他们不赞成这样做,这也是一个惊喜。好的,幸好还有window.onload
【解决方案4】:

试试这个:

参考:http://api.jquery.com/load-event/

$(document).ready(function() {
    $("#aspnetForm img").load(function() {
        alert($(this).height());
        alert($(this).width());
    });
});

这是另一种方法:DEMO

function imageSize(img){
   var theImage = new Image();
   theImage.src = img.attr('src');
   var imgwidth = $(img).width();
   var imgheight = $(img).height();

   alert(imgwidth);
   alert(imgheight);
}

$('img').each(function(){
    var imgsrc = jQuery(this);
    imageSize(imgsrc);
});

【讨论】:

  • load event img bind 在某些浏览器中不起作用,例如某些版本的 Opera 11,12。如果图像很少会发生什么
猜你喜欢
  • 2012-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-15
  • 2012-06-06
  • 2011-04-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多