开发者

Getting the Image Source from an HTMLImageElement object

I have the following jQuery which I want to output a list of images. The problem is that I cannot get 'this' to find the source. It cu开发者_运维技巧rrently is an object which outputs as an HTMLImageElement. How can I get the image source from this object?

$("#imgs li.images img").each(function(i) { 
    $("#list").append("<li><img src=\""+this.attr("src")+"\" /></li>");
});

I currently get the error that this.attr is not a function.


this is the DOMNode, not a jQuery object. You can access this.src immediately, or, if you want to use jQuery, $(this).attr('src'), although would be a detour for doing the same thing.


Use $(this) instead of this.


The problem is your use of 'this'. It is a special keyword used in object orientated programming to refer to an objects own properties

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜