我正在尝试显示高度和高度div内某个图像的宽度。然而它现在显示'undefined'。我想这与onload是一个ASYN函数有关。我对coffeescript / javascript很新,我正在努力学习它。但是如何启用回调?
getMeta = (url, callback) ->
img = new Image()
img.src = url
console.log(img)
img.onload = ->
console.log("load")
width = @width
height = @height
imageUrl = "http://imgcdn.igdb.com/images/limbo/6843_screenshot_show_6843_at_igdb_com.jpg"
meta = getMeta imageUrl
$("#debug").html("Height: " + meta.height + "px W: " + meta.width + "px")
答案 0 :(得分:0)
但是,如果你真的希望它在顶部,请尝试:
$("document").ready(function() {
$("#click").click(function() {
var img = new Image();
img.onload = function() {
$("#debug").html("Height: " + this.height + "px W: " + this.width + "px")
}
img.src = $("#img_url").val();
});
});
和HTML
<input id="img_url" type="text">
<div id="click">Click Here</div>
<div id="debug"></div>