开发者

How to wrap var img = new image with tag?

开发者 https://www.devze.com 2023-03-21 11:13 出处:网络
Trying to preload images in a loop and it works fine but how to wrap each image with another tag. Doing this code won\'t work and I can\'t see a problem so far.

Trying to preload images in a loop and it works fine but how to wrap each image with another tag. Doing this code won't work and I can't see a problem so far.

...
var img = new Image();
$(img).load(function(){
$(this).hide();         
$('.container').append('<a href="/">'+this+'</a>');                                                     $(this).fadeIn();   开发者_StackOverflow社区                                    
});
...


have you tried using $(this) rather than this.

or why dont you use:

  var $html = $(this).wrap('<a href="/" />');
  $('.container').append($html);


You can't do '<a href="/">'+this+'</a>' when this is a DOMElement.

A good solution can be:

// Inside the callback
$('<a href="/"></a>').append(this).appendTo('.container');

Remember that if you use .wrap it will return the original element, not the wrapper!


you can use wrap

http://api.jquery.com/wrap/


It doesn't know what to do w/ "this"... If you're not doing other further manipulation w/ the image at this step is it possible that you do this:

...
$('.container').append('<a href="/"><img id=' + anIdOfYourChoosing + ' /></a>');
...

and you just retroactively set properties on the image after the fact.


Use $.wrap

$(img).wrap('<a href="#"></a>');
0

精彩评论

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