我正在抓取各种 jquery 对象并将其放入数组中,然后以 HTML 形式输出。
我正在尝试将 jQuery 对象转换为文本字符串,以便稍后可以将其输出为 HTML。
我现在正在使用这种技术:
console.log($myObject.clone().wrap('<div></div>').html());
但是,这似乎只是抓取了我的对象的内容。
例如,如果 $myObject 是 <h2>My Title</h2>
以上仅返回“我的标题”(无 H2 标签)。
我也尝试使用 .text() 但得到相同的结果。
有没有办法将整个 jQuery 对象转换为文本字符串?
最佳答案
好吧,回答我自己的问题。
console.log($('<div>').append($myObject.clone()).remove().html());
感谢 John Feminella 在该帖子中的贡献:
How do you convert a jQuery object into a string?
谁引用了这篇文章:
http://jquery-howto.blogspot.com/2009/02/how-to-get-full-html-string-including.html
关于jquery - 将 jQuery 对象和内容转换为字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2000438/