简体   繁体   English

将jQuery元素转换为html字符串

[英]Convert jQuery element to html string

This JS code tries to modify the raw html. 此JS代码尝试修改原始html。 It does that by converting the html to jQuery element, does the modifications on the jQuery element then the part which is not working is converting back to raw html string. 它通过将html转换为jQuery元素,对jQuery元素进行修改,然后将不起作用的部分转换回原始html字符串。

Since .html() will not work with xml as indicated in the docs 由于.html()不适用于docs中指示的xml
How can it convert the jQuery back to raw html string? 如何将jQuery转换回原始html字符串? Thanks 谢谢

let jQ = $($.parseHTML(raw_html));
//modify jQ to heart content
console.log(jQ.html()); //<-- undefined

The raw_html raw_html

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
//...

</html>

edit 编辑
Output of console.log($.parseHTML(raw_html)); console.log($.parseHTML(raw_html));
在此处输入图片说明

Do var a = $('<div>').append(raw_html); var a = $('<div>').append(raw_html);

//do modifications to variable a //对变量a进行修改

$(a).html() will display the correct html $(a).html()将显示正确的html

NOTE this will strip head and other tags as discussed here 注意:这将去除此处讨论的头部和其他标签

heres a plnkr 继承人plnkr

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM