I don’t understand very well this code:
var img = $('<img/>', {class: "photo", src: photo.url_n, width: wt, height: ht}).css("margin", border + "px");
It seems to create an image tag: <img src="" class="photo" ... />. I just want to wrap it with a link: <a href="#aaaa"></a>
But I am trying this and it doesn’t work:
$(img).wrap('<a></a>');
I have also tried just:
img.wrap('<a></a>');
Finally the code add the img to another element like this:
d_row.append(img);
First: since
imgis created as a jQuery object, you don’t need to write$(img)and wrap it in a jQuery object again. Justimgwill suffice.Second: if
imghasn’t beenappended to the document yet, then you can’t modify it in-place withwrap().Third:
wrap()is designed to return the contents of the wrapped object, not the wrapper itself. You need to callparent()to get that.Try:
(you don’t need the closing
</a>, jQuery will generate it automatically)http://jsfiddle.net/TMeP6/
However: since
imgis no longer an image, the variable nameimgis inaccurate and potentially confusing. I would create a new variable name (a_imgor something) and store it in that instead: