开发者

jquery .html removing img closing tag?

I'm using jquery to both set html with an i开发者_运维技巧mg tag and later retrieve html with an img tag and store it in a datastore.

When I initially set html, the string I'm passing in has a closing tag:

h += '<div class="photo">'
+ '<img src="' + msg + '" />'
+  '</div>';
$('#thumb').html(h);

When I retrieve the html later, it does not have a closing tag.

This is causing issues when I try serve this html at a later date.

$('div.thumbnail').html() 

produces html like this without the closing tag:

<div class="photo">
   <img src="http://lh3.ggpht.com/blah.jpg">
</div>

EDIT

Thanks for all the suggestions. In my case I kept getting an "expected </img>" error when trying to serve the HTML back.

So I changed my approach and used this code to retrieve the img src, rebuild the html, and then store it:

var xml = '<?xml version="1.0" encoding="utf-8"?>\n<content>\n';
var img = $("#thumbimage").attr("src");

xml += '<thumbURL><div class="photo"><img src="' + img +  '"/></div>'
    + '</thumbURL>\n';


What problem is this causing specifically, to be honest i'd expect an unclosed img tag to render OK in all browsers.

.html() with no arguments is basically just a wrapper for .innerHTML, the results may vary by browser but it isn't a jquery issue


You should us jQuery a bit more to make it easier.

var content = $("<div>").addClass("photo").append($("<img>").attr("src", msg));
$("#thumb").html(content);

jQuery will build these tags for you. http://api.jquery.com/jQuery/#jQuery2

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜