没有得到一个变量值? [英] Not getting a variable value?

查看:69
本文介绍了没有得到一个变量值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在加载函数中没有获得"img_srt"变量的值,有人可以帮我吗?

I am not getting the value of the variable of "img_srt" in load function, can any body helps me?

$(document).ready(function() {
    Get_var();
})

var img_srt='<div>address</div>';   

function Get_var() {
    $("<img />").attr("src","http://www.google.com/intl/en_ALL/images/logo.gif")
        .load(function() {
            if (this.height > 0) {
                img_srt += "<div><img src='http://www.google.com/intl/en_ALL/images/logo.gif'/></div>";         
            }
        })
    alert(img_srt);
}

推荐答案

设置img_srt的函数被异步调用.也就是说,它可以在您的alert语句之前或之后执行.可能之后,因为您遇到了这个问题.

The function which sets img_srt is called asynchronously. That is, it may be executed before or after your alert statement. Likely after, as you're having this problem.

尝试这样的事情:

$('<img/>')
    .attr('src', 'http://www.google.com/intl/en_ALL/images/logo.gif')
    .load(function() {
        var imageLoaded = this.height > 0;

        if(imageLoaded) {
            $(this).appendTo('#myDiv');
        } else {
            $('<p/>').text('Unable to load image').appendTo('#myDiv');
        }
    });

这篇关于没有得到一个变量值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆