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 用三极管设计—个共射极放大电路
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示