dso0139 2017-02-16 08:37
浏览 58
已采纳

jQuery-Ajax在Ajax-Container中获取Image的宽度[复制]

This question already has an answer here:

I have created a page with Ajax function (jQuery & PHP). My problem is when I load content via Ajax in my page. I want to read width of images placed inside of Ajax-container.

I noticed something quite remarkable.

My script:

<script>
 $(window).ready(function () {
  var bildBreite = $("#wrapper > #target-image").width();
   alert(bildBreite);
  $("#wrapper > #image-wrapper").css({"width": bildBreite});
 });
</script>

It does not work, but when I insert alert ("load"); then work it.

<script>
 $(window).ready(function () {
  alert("Load");
  var bildBreite = $("#wrapper > #target-image").width();
   alert(bildBreite);
  $("#wrapper > #image-wrapper").css({"width": bildBreite});
 });
</script>

What happens here, how can I read the image width?

</div>
  • 写回答

1条回答 默认 最新

  • doumo3903 2017-02-16 08:40
    关注

    In that case you need success callback because AJAX is Asynchronous so on Window Load it will not the get the element width so try it like,

    $.ajax({
        ...
        success:function (response) {
           var bildBreite = $("#wrapper > #target-image").width();
           $("#wrapper > #image-wrapper").css({"width": bildBreite});
        }
    });
    

    Read more about jQuery.ajax

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失