Tag: 未定义的

jQuery:如何访问外部变量?

我处在一种需要用这种方式解决的情况; 需要将local variable转换为global variable 。 有一个例子返回图像的真实宽度和高度,我从这个答案中找到了这些方法。 。 需要将本地变量pic_real_height和pic_real_width转换为全局变量并返回其真值。 这是jsFiddle。 CSS: img { width:0px; height:0px; }​ jQuery: console.log($(‘.imgCon img’).height());//returns 0 var img = $(‘.imgCon img’)[0]; // Get my img elem var pic_real_width, pic_real_height; $(”).attr(‘src’, $(img).attr(‘src’)).load(function() { pic_real_width = this.width; pic_real_height = this.height; console.log( pic_real_width + ‘x’ + pic_real_height ); // — returns true 570×320 — }); […]