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>

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