You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

amp-script中innerHTML原样渲染转义序列问题求解决方案

Fixing innerHTML Entity Parsing Issue in amp-script

Ah, I've run into this exact quirk with amp-script before! The issue here is that AMP's sandboxed execution environment for amp-script alters the standard behavior of innerHTML: instead of parsing HTML entities like &lt; and &amp; into their actual characters (< and &), it escapes them again, resulting in the raw entity strings being rendered.

Here are two reliable fixes you can use:

1. Use DOMParser to parse the entity string into a DOM node

This method explicitly parses your HTML entity string into proper DOM content before inserting it into the target element:

const parser = new DOMParser();
// Parse the entity string as HTML
const parsedDoc = parser.parseFromString('&lt;&amp;', 'text/html');
// Append the parsed content to your target span
document.getElementById("a").appendChild(parsedDoc.body.firstChild);

2. Use a temporary element to parse entities first

You can also create an off-screen temporary element, let it parse the entities via innerHTML, then move its content to your target element:

const tempContainer = document.createElement('div');
// Let the temp element parse the entities
tempContainer.innerHTML = "&lt;&amp;";
// Move the parsed text node to your target span
document.getElementById("a").appendChild(tempContainer.firstChild);

Modified Full Example Code

Here's your original example updated with the first fix:

<!doctype html>
<html data-ampdevmode ⚡>
<head>
    <meta charset="utf-8">
    <link rel="canonical" href="self.html">
    <meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
    <style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style>
    <noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
    <script async src="https://cdn.ampproject.org/v0.js"></script>
    <script async custom-element="amp-script" src="https://cdn.ampproject.org/v0/amp-script-0.1.js"></script>
</head>
<body data-ampdevmode>
    <amp-script layout="fixed-height" height="800px" script="hello-world">
        <span id="a"></span>
    </amp-script>
    <script id="hello-world" type="text/plain" target="amp-script">
        const parser = new DOMParser();
        const parsedDoc = parser.parseFromString('&lt;&amp;', 'text/html');
        document.getElementById("a").appendChild(parsedDoc.body.firstChild);
    </script>
</body>
</html>

Both approaches work because they bypass the AMP sandbox's extra escaping layer by letting the browser's native HTML parser handle the entities first, then inserting the already-parsed content into the DOM.

内容的提问来源于stack exchange,提问作者user3612643

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 20:59:08