javascript堆叠错误

squ*_*ker -1 javascript jquery

我有这个代码:

 $jq('.pop-tip').click( function(event) {
        width = '';
        height = '';
        var img = new Image();
        img.onload = function() {
            width = 'width='+(this.width + 240);
            height = 'height='+(this.height + 360);              
        }
        img.src = 'http://www.domain.com/uploads/2011/10/dealoftheweek.jpg';        
        destination = $jq(this).attr('title');
        postPop = $jq(this).attr('href');
        alert ( width );
        window.open(postPop, 'tip_pop',''+ width +', '+ height +', scrollbars=0' ).focus();
        window.open(destination, '_parent','' );
        event.preventDefault();
)};
Run Code Online (Sandbox Code Playgroud)

出于某种原因,它只适用于窗口上方的alert().打开调用为什么会这样,我该如何解决?

Bir*_*rey 6

这是因为你在onload事件中设置高度和宽度,只有在图像完全加载后才会出现.发出警报将使图像完全加载,当您单击警报确定时,将调用图像的onload并设置值.但是,如果你删除它,高度和宽度值没有设置,所以它不起作用.

你可以通过调用onload事件中的其余代码来解决这个问题.