JQuery.html () returns invalid IMG

Possible duplicates:
jQuery html () and self-closing tags.
Is jQuery $ ('span') expected. Does html () turn an XHTML br tag into html syntax?

I have such a document

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script src="/js/core/jquery.js" type="text/javascript"></script>
</head>
<body>
    <div id="content">
        <img src="/some/image.gif" />
    </div>
</body>
</html>

I want to extract html for some nodes in order to do something. But I see one problem with IMG.

$('#content').html();

This returns invalid XHTML

<img src="/some/image.gif">

JQuery version is 1.4.2.

+5
source share
3 answers

This is a duplicate, but none of the answers in the original question give an actual workaround. (This is probably true. I cannot disagree with what @Cletus says in his - as always excellent - answer.)

XHTML: jQuery, innerXHTML() ( , 2007 , Beta) JavaScript- innerXHTML, promises , . , .

+5

innerHTML/html() XHTML, XHTML, application/xhtml+xml. ( IE < 9.) text/html, - , , DOM. HTML DOM, , .

, IE innerHTML HTML: attr , . , innerHTML, - . , , , . html() . , , , innerHTML , .

XHTML? , , DOM-.

ETA :

XHTML . , HTML , html(). :

, html(), - , . , <img title="Hello, this is some description. Another sentence."> - , <span> title, .

, , . , jQuery . . findText , :

// Split each text node into things that look like sentences and wrap
// each in a span.
//
var element= $('#content')[0];
findText(element, /.*?[.?!]\s+?/g, function(node, match) {
    var wrap= document.createElement('span');
    node.splitText(match.index+match[0].length);
    wrap.appendChild(node.splitText(match.index));
    node.parentNode.insertBefore(span, node.nextSibling);
});
+5

.html() HTML XHTML, ​​ , . XHTML - .

HTML XHTML , DOM. DOM , HTML, HTML XHTML. DOM , HTML , .

+3

All Articles