将jQuery对象和内容转换为字符串? [英] Convert a jQuery object and contents to string?

查看:731
本文介绍了将jQuery对象和内容转换为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在抓取各种jquery对象放入一个数组中,然后将它们作为HTML吐出来.

I'm grabbing various jquery objects to put into an array and later spit out as HTML.

我正在尝试将jQuery对象转换为文本字符串,以便以后可以将其作为HTML吐出.

I'm trying to convert the jQuery object to a text string so it can be spit out as HTML later.

我目前正在使用这种技术:

I'm using this technique at the moment:

console.log($myObject.clone().wrap('<div></div>').html());

但是,那似乎只是在抓取我对象的内容.

However, that only appears to be grabbing the contents of my object.

例如,如果$ myObject是<h2>My Title</h2>,则上面的代码仅返回我的标题"(没有H2标签).

For example, if $myObject is <h2>My Title</h2> the above only returns 'My Title' (no H2 tags).

我也尝试使用.text(),但得到相同的结果.

I've also trying using .text() but get the same result.

是否可以将整个jQuery对象转换为文本字符串?

Is there a way to convert the entire jQuery object into a text string?

推荐答案

好,回答我自己的问题.

Well, answering my own question.

console.log($('<div>').append($myObject.clone()).remove().html());

在此主题中感谢John Feminella:

Thanks goes to John Feminella in this thread:

如何将jQuery对象转换为字符串?

谁引用了这篇文章:

http://jquery-howto.blogspot.com/2009/02/how-to-get-full-html-string-includes.html

这篇关于将jQuery对象和内容转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆