开发者

jQuery: html() returning empty string

开发者 https://www.devze.com 2023-01-12 07:02 出处:网络
If I use firebug and ty开发者_JAVA技巧pe $(\"<p>\").html() into the watch window, I get an empty string back. My guess is because the $(\"<p>\") hasn\'t been rendered to the document. How

If I use firebug and ty开发者_JAVA技巧pe $("<p>").html() into the watch window, I get an empty string back. My guess is because the $("<p>") hasn't been rendered to the document. How can I get the markup for $("<p>") before it's added to the DOM?


You're asking for the outer HTML, not the inner, the HTML inside the <p> is indeed empty.

To get the HTML you'd have to wrap it in another element, like this:

jQuery("<div>").append($("<p>").clone()).html()

This would give you:

<p></p>


Try $( 'p' ).html() rather than $( '<p>' ).html()

Example at http://jsfiddle.net/tdg7U/

Test1 alerts $( '<p>' ).html()

Test2 alerts $( 'p' ).html()

0

精彩评论

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