string 如何将html对象转换为字符串类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12344832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert the html object to string type?
提问by hh54188
I use jQuery method to get some type of html object:
我使用 jQuery 方法来获取某种类型的 html 对象:
var content = $('#cke_ckeditor iframe').contents().find('.cke_show_borders').clone();
Then I want to convert it to string
type:
然后我想将其转换为string
类型:
console.log(content[0].toString());
but the the result is:
但结果是:
[object HTMLBodyElement]
How can I turn it into real string?
我怎样才能把它变成真正的字符串?
By the way, can I turn the converted html string to the html object?
顺便说一句,我可以将转换后的html字符串转换为html对象吗?
回答by Chris
I believe you want to use Element.outerHTML:
我相信你想使用Element.outerHTML:
console.log(content.outerHTML)
console.log(content.outerHTML)
回答by E. Nat
I had the same problem.
我有同样的问题。
var docString = "<html>"+content.documentElement.innerHTML+"</html>"
var docString = "<html>"+content.documentElement.innerHTML+"</html>"
回答by Vijay Maurya
The correct way to Convert a jQuery object into a string:
将 jQuery 对象转换为字符串的正确方法:
var content = $('#cke_ckeditor').find('.cke_show_borders').eq(0).clone();
var content = $('#cke_ckeditor').find('.cke_show_borders').eq(0).clone();
console.log(content.get(0).outerHTML);
console.log(content.get(0).outerHTML);
回答by jdelobel
You can try the following:
您可以尝试以下操作:
content.text();
回答by Cybersupernova
You can use this content[0].prop('outerHTML')
你可以用这个 content[0].prop('outerHTML')
It worked for me
它对我有用
Reference : How do you convert a jQuery object into a string?