gpt4 book ai didi

javascript - 使用 jQuery 读取 div 内容会跳过 HTML、head 和 body 标签

转载 作者:太空宇宙 更新时间:2023-11-04 16:06:36 25 4
gpt4 key购买 nike

我正在读取一个 HTML 模板,并将其从服务器端代码后面添加到 div(我已设置 runat='server')。这可以完美地在页面上呈现并显示 HTML。现在,我更改客户端渲染的 HTML 中的文本,并希望在服务器端读取更新的文本(div 内的完整 HTML)以更新模板。因此,我采用了一个隐藏字段,并在单击“保存”按钮时保存了 div 的更新 HTML。

$('#btnSaveTemplate').click(function () {
$('#UpdatedEmailTemplate').val($('#divEmailBody').html());
});

HTML 模板:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<p>
<strong><u>Database Decommission Notice – IMPORTANT NOTIFICATION</u></strong>
</p>
<p>
<strong>FOLLOWING DATABASE(S) ARE SCHEDULED TO BE DECOMMISSIONED in 4 WEEKS from {SentDate};</strong>
</p>

<p>
<strong>Why have you received this email?</strong>
</p>
<p>
You are receiving this email as because you are an ITAO/ITAO delegate/CSM/CSM-1/key technical contact for database(s) that are scheduled to be decommissioned by 30. Below are the applications you have been identified.
</p>
</body>
</html>

问题是,当我尝试在单击保存的隐藏字段中保存更改后的 html 时,它会给出如下所示的内容。 (这是 $('#divEmailBody').html() 方法的输出)

\n\n\n    <title></title>\n\n\n    <p>\n        <strong>\n            <img style=\"width: 744px; height: 141px;\" alt=\"\" src=\"http://nyccgdbb0004.us.db.com/images/emailImmediateActionRequired.png\" border=\"0\">\n        </strong>\n    </p>\n    <p>\n        <strong><u>Database Decommission Notice – IMPORTANT NOTIFICATION</u></strong>\n    </p>\n    <p align=\"center\" class=\"MsoNormal\" style=\"margin: 0in 0.5in 6pt 0in; text-align: center;\">\n        <b>\n            <i>\n                <u>\n                    <span lang=\"EN-GB\" style=\"color: rgb(192, 0, 0); font-size: 14pt;\">Do not ignore this message</span>\n                </u>\n            </i>\n        </b>\n    </p>\n    <p>\n        <strong>FOLLOWING DATABASE(S) ARE SCHEDULED TO BE DECOMMISSIONED in 4 WEEKS from 1/23/2017;</strong>\n    </p>\n   \n    <p>\n        <strong>Why have you received this email?</strong>\n    </p>\n    <p>\n        <a title=\"\" class=\"editableText editable editable-pre-wrapped editable-click editable-unsaved\" href=\"#\" data-original-title=\"\" data-title=\"Update Text\" data-type=\"textarea\">You are receiving this email as because you are an ITAO/ITAO delegate/CSM/CSM-1/key technical contact for database(s) that are scheduled to be decommissioned by 30. Below are the applications you have been identified.test</a>\n    </p>\n\n\n\n"

如何阅读完整的 HTML?

最佳答案

如果它是模板,那么不要将其添加到任何 html 元素,如 div.. 而是使用 script 标签。

<script id="UpdatedEmailTemplate" type="text/x-template">
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<p>
<strong><u>Database Decommission Notice – IMPORTANT NOTIFICATION</u></strong>
</p>
</body>
</html>
</script>

如果您将模板附加到 div,它不会保持原样,浏览器会自行调整以使页面成为有效的 html。在一个页面中不能有两个 html 标签,因此浏览器会自动删除另一个标签。 body 也一样。 HTML 永远不会抛出错误,但它会自行修复错误。有时修复与我们想要的不一样。这就是为什么它需要有效的 html

关于javascript - 使用 jQuery 读取 div 内容会跳过 HTML、head 和 body 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41813583/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com