0

だから私は jquery で作成された img 要素の高さと幅を読み取ろうとしています。

これは私が実行しようとしているコードです

$(document).ready(function() 
{
    $.ajax({
            url: 'res/script/getImage.php', 
            type: 'POST',
            data: {
                param  : 'Load'
            },
            success: function(result)
            {
                ImageArray = JSON.parse(result);
               for(i=0; i<ImageArray.length; i++)
                {
                        //Read the Image element and update to gallery

                        document.getElementById("gallery").innerHTML += ImageElement;
                        ImageArrayComplete[ImageCounter] = ImageArray[i].image;
                        UserArrayComplete[ImageCounter]  = ImageArray[i].user;

                }

             //Create Canvas
            $('#gallery img').each(function() {
                console.log(this);
                console.log('Width=' + this.width+ 'Height='+this.height);
                createCanvas(this);
            });

            }
        });
});

の出力console.log(this);は、期待どおりの画像のパスです。

ただし、console.log('Width=' + this.width+ 'Height='+this.height);最初のページの読み込み中は、幅と高さの両方で の出力が 0 になります。

ページが更新されると、値は実際の幅の 200 にリセットされます。

他にスローされるエラーはなく、firefox と chrome の両方で発生します。ここで何が欠けていますか?

4

2 に答える 2

0

img select に高さと幅の値がありますか?

于 2013-02-18T04:45:08.740 に答える
0

あなたのImageElementオブジェクトは未定義です。これを試して:

変化する

document.getElementById("gallery").innerHTML += ImageElement;

$('<img/>').attr('src', ImageArray[i].image).appendTo($('#gallery'));

andオブジェクトにはand属性thisがありません。高さを取得したい場合は、次を使用します。heightwidth

console.log('Width=' + $(this).width() + 'Height=' + $(this).height());
于 2013-02-18T04:52:36.103 に答える