使用 Javascript 的 innerHTML 时如何按原样保留 html

发布于 2024-12-06 02:28:50 字数 1908 浏览 1 评论 0原文

一切工作正常,decodeURIComponent(xmlhttp.responseText) 从解析脚本返回为 html,经警报检查,控制台中没有错误,但将 html 替换为编辑器

retHTML.innerHTML =decodeURIComponent(xmlhttp.responseText);

似乎具有将我的 html 翻译为编码的“功能” html(&&) 等.. 我不确定为什么说它是内部 html 的命令会阻止您实际使用 html? insideText 只是放大了这一点,而不是返回隐含的 html 自由文本。如何将返回的 html 返回到 div 作为我请求的 html?

function setHTML(retHTML)
{
retHTML.innerText =retHTML.innerHTML; }

function setTEXT(retHTML)
{
var tmpTXT =encodeURIComponent(retHTML.innerText);
var xmlhttp;
xmlhttp=new XMLHttpRequest(); 
xmlhttp.onreadystatechange=function()
{ retHTML.innerHTML =decodeURIComponent(xmlhttp.responseText);  }
xmlhttp.open("POST","path.php",true);
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlhttp.send('send=' + tmpTXT); }

测试标记

<object width="853" height="510">
<param name="movie" value="http://www.youtube.com/v/iaysTVcounI?version=3&amp;hl=en_US&amp;rel=0">
<param name="allowFullScreen" value="true">
<param name="allowscriptaccess" value="always">
<embed src="http://www.youtube.com/v/iaysTVcounI?version=3&amp;hl=en_US&amp;rel=0&amp;autoplay=1" type="application/x-shockwave-flash" width="853" height="510" allowscriptaccess="always" allowfullscreen="true">
</object> 

如下

<object width="853" height="510">
<param name="movie" value="http://www.youtube.com/v/iaysTVcounI?version=3&hl=en_US&rel=0">
<param name="allowFullScreen" value="true">
<param name="allowscriptaccess" value="always">
<embed src="http://www.youtube.com/v/iaysTVcounI?version=3&hl=en_US&rel=0&autoplay=1" type="application/x-shockwave-flash" width="853" height="510" allowscriptaccess="always" allowfullscreen="true">
</object>

everything is working fine, decodeURIComponent(xmlhttp.responseText) comes back as html from parsing script as checked with alert, no errors in the console, but replacing the html to the editor with

retHTML.innerHTML =decodeURIComponent(xmlhttp.responseText);

seems to have a "feature" of translating my html to encoded html(& to &) etc..
I'm not sure why a command that says it is the inner html would prevent you from actually using html? innerText simply amplifies this rather then returning the html free text as implied. How do I get the returned html back to the div as the html I am requesting?

function setHTML(retHTML)
{
retHTML.innerText =retHTML.innerHTML; }

function setTEXT(retHTML)
{
var tmpTXT =encodeURIComponent(retHTML.innerText);
var xmlhttp;
xmlhttp=new XMLHttpRequest(); 
xmlhttp.onreadystatechange=function()
{ retHTML.innerHTML =decodeURIComponent(xmlhttp.responseText);  }
xmlhttp.open("POST","path.php",true);
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlhttp.send('send=' + tmpTXT); }

test markup as follows

<object width="853" height="510">
<param name="movie" value="http://www.youtube.com/v/iaysTVcounI?version=3&hl=en_US&rel=0">
<param name="allowFullScreen" value="true">
<param name="allowscriptaccess" value="always">
<embed src="http://www.youtube.com/v/iaysTVcounI?version=3&hl=en_US&rel=0&autoplay=1" type="application/x-shockwave-flash" width="853" height="510" allowscriptaccess="always" allowfullscreen="true">
</object> 

should be

<object width="853" height="510">
<param name="movie" value="http://www.youtube.com/v/iaysTVcounI?version=3&hl=en_US&rel=0">
<param name="allowFullScreen" value="true">
<param name="allowscriptaccess" value="always">
<embed src="http://www.youtube.com/v/iaysTVcounI?version=3&hl=en_US&rel=0&autoplay=1" type="application/x-shockwave-flash" width="853" height="510" allowscriptaccess="always" allowfullscreen="true">
</object>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美人如玉 2024-12-13 02:28:50

innerHTML 在写入时是一个解析器,在读取时是一个序列化器,而不是存储文本的简单属性。

当您写入 innerHTML 时,它会将 HTML 源文本转换为 DOM 对象,例如 Element、Attr 和 TextNode。这些 DOM 对象反映了浏览器窗口中文档的真实状态,而不是任何看起来像 HTML 源的东西。

写入时,任何对 DOM 中存储的“信息集”不重要的信息都会丢失。这包括大小写(无论您的标签是 还是 )、属性顺序、标签内的空格、实体和字符引用(即是否你说的是 cafécafécafé——对于 HTML 解析器来说都是一样的),等等 更多的。

读回您刚刚编写的 innerHTML 将重新序列化已解析的 DOM,并返回一些内容,但不一定与您输入的字符串相同。如果您在原始 HTML 中犯了错误,您还会发现输出已被更正,以反映 HTML 解析器所做的操作,以便能够读取您输入的内容。因此,无效的裸与符号必然会变成 &

(我不确定您指的是哪种“编辑器”,但如果它是

预计到达时间:

应为 value="http://www.youtube.com/v/iaysTVcounI?version=3&hl=en_US&rel=0"

不,不应该。这不是有效的 HTML。将 & 转义为 & 绝对是正确的、必需的事情。

innerHTML is a parser when you write to it and a serialiser when you read, not a simple property that stores text.

When you write to innerHTML, it converts the HTML source text into DOM objects like Element, Attr and TextNode. It is these DOM objects that reflect the real live state of the document in the browser window, and not anything that looks like HTML source.

On writing, any information that is not significant to the ‘information set’ stored in the DOM is lost. This includes things like case (whether your tag was <b> or <B>), order of attributes, whitespace inside tags, entity and character references (ie whether you said café, café or café—it's all the same to an HTML parser), and much more.

Reading back the innerHTML you just wrote will re-serialise the parsed DOM and give you something back that won't necessarily be the same string as what you put in. If you made mistakes in your original HTML, you'll also find that the output has been corrected to reflect what the HTML parser did to be able to read what you put in. So an invalid bare ampersand will necessarily turn into &.

(I'm not sure what kind of ‘editor’ you're referring to, but if it's a <textarea> then that will additionally fix up < to < in any HTML you write to it, because a textarea can't contain markup. It's also rather odd and unnecessary that you would be passing back URL-encoded data in a responseText; are you sure that's what's happening?)

ETA:

should be value="http://www.youtube.com/v/iaysTVcounI?version=3&hl=en_US&rel=0"

No it shouldn't. That's not valid HTML. Escaping the &s to & is absolutely the correct, required thing to do.

没︽人懂的悲伤 2024-12-13 02:28:50

我觉得这有点贫民窟,但最终以下将是我的解决办法。我建议将此作为innerHTML 的替代方案,用于字符串的文字替换。

retHTML.innerHTML ='';  
retHTML.execCommand('insertHTML',false,xmlhttp.responseText);

I feel this is kinda ghetto but ultimately the following will be my fix. I recomend this as an alternative to innerHTML for literal replacement of the string.

retHTML.innerHTML ='';  
retHTML.execCommand('insertHTML',false,xmlhttp.responseText);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文