How to convert the html object to string type?

社会主义新天地 提交于 2020-06-09 11:53:25

问题


I use jQuery method to get some type of html object:

var content = $('#cke_ckeditor iframe').contents().find('.cke_show_borders').clone();

Then I want to convert it to string type:

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?


回答1:


I believe you want to use Element.outerHTML:

console.log(content.outerHTML)




回答2:


I had the same problem.

var docString = "<html>"+content.documentElement.innerHTML+"</html>"




回答3:


You can try the following:

content.text();



回答4:


You can use this content[0].prop('outerHTML')

It worked for me

Reference : How do you convert a jQuery object into a string?




回答5:


The correct way to Convert a jQuery object into a string:

var content = $('#cke_ckeditor').find('.cke_show_borders').eq(0).clone();

console.log(content.get(0).outerHTML);



来源:https://stackoverflow.com/questions/12344832/how-to-convert-the-html-object-to-string-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!