获取新创建元素的属性



我正在尝试获取加载后添加到页面的图像的src属性,但结果一直返回undefined.

$('#qtr').change(function() {
    data = {
        'img': $('#edit_image').attr('src'),
        'degrees': '90'
    };
    $.post('/ajax/ajax_images/rotate', data, function(response) {
        if (response.error != 0) {
            alert('Unable to rotate image');
        }
        else {
            $('#edit_image').attr('src', response.html);
            new_image = response.html;
            var start = new_image.lastIndexOf('/') + 1;
            new_image = new_image.substr(start);
        }
    }, 'json');
});​

$('#edit_image')是新创建的映像。我哪里出错了?

问题是一旦 DOM 加载了对 DOM 的任何修改,就需要明确告知 jquery 有关更改的信息。因此,使用回调函数查看 .bind() 或 .live(),具体取决于您的 jquery 版本。

尝试:

$('img:last' ).attr('src')

;-)

最新更新