简体   繁体   English

如果我将某些内容写到innerHTML中,如何获得准确的副本?

[英]If I write something into innerHTML, how do I get exact copy back?

I have string and I want to write this string into innerHTML property and then later on I want to compare these two strings. 我有字符串,我想将此字符串写入innerHTML属性,然后稍后再比较这两个字符串。 Basicaly I want something like this: 基本上我想要这样的东西:

var myString = "<div>my message with accented áé characters</div>"
var output = document.getElementById("first")
output.innerHTML = myString

Then do comparison 然后做比较

if (output.innerHTML == myString) {
...
}

by running this code I get false result for the string above because sequence áé 通过运行此代码,我得到上述字符串的错误结果,因为序列áé gets translated to áé and thus strings don't match. 被翻译成áé ,因此字符串不匹配。 The closest I got to desired result was to use little hack and create temporary element write to it and then compare with data extracted from it. 我最接近期望结果的地方是使用一点技巧,并向其中写入临时元素,然后与从中提取的数据进行比较。 It works fine, but it doesn't feel right to do it this way. 它可以正常工作,但是用这种方式这样做不合适。

var tmp = document.createElement('div');
tmp.innerHTML = myString

if (output.innerHTML == tmp.innerHTML) {
...
}

So my question is: 所以我的问题是:

  • is there any way how to get back exact copy of what I wrote into innerHTML? 有什么办法可以找回我写进innerHTML的确切副本吗?
  • or is there a simple way to transform data, so they match? 还是有一种简单的方法来转换数据,以便它们匹配?
  • or is my hack just standard way how to do this kind of things? 还是我的hack只是如何做这种事情的标准方法?

To put it into context I am generating data, then transform it into JSON. 为了将其置于上下文中,我正在生成数据,然后将其转换为JSON。 This JSON output is then read in javascript by JSON.parse() and result is inserted into various tags. 然后,可以通过JSON.parse()在javascript中读取此JSON输出,并将结果插入到各种标签中。 I am checking difference first, because when new data differs from old data, then other actions take place too. 我首先检查差异,因为当新数据与旧数据不同时,其他操作也会发生。 I have also tried things like unescape() but it doesn't work on this kind of escaping. 我也尝试过unescape()类的事情,但是在这种转义上不起作用。 And yes, there is still option to store raw strings in Map and compare these. 是的,仍然可以选择将原始字符串存储在Map并进行比较。

Here is little more complete demonstration code which I also put on JSFiddle 这是我也放在JSFiddle上的更完整的演示代码

<div id="first"> </div>
<div id="resultFirst"></div>
<br />
<div id="second"> </div>
<div id="resultSecond"></div>

<script>
var myString = "&lt;div&gt;my message with accented &#225;&#233; characters&lt;/div&gt;"
var output = document.getElementById("first")
var result = document.getElementById("resultFirst")

output.innerHTML = myString
if (output.innerHTML == myString) {
  result.innerText = "TRUE " + myString
} else {
  result.innerText = "FALSE " + myString
}


var output2 = document.getElementById("second")
var result2 = document.getElementById("resultSecond")

var tmp = document.createElement('div');
tmp.innerHTML = myString

output2.innerHTML = myString
if (tmp.innerHTML == output2.innerHTML) {
  result2.innerText = "TRUE " + tmp.innerHTML
} else {
  result2.innerText = "FALSE " + tmp.innerHTML
}
</script>

The short answer is no; 最简洁的答案是不; The HTML is just a display layer and you shouldn't rely on it for storing data, only showing it. HTML只是显示层,您不应该依赖它来存储数据,而只能显示它。

The browser will do whatever it needs to the innerHTML to make it renderable. 浏览器将对innerHTML做任何需要的事情,以使其可呈现。 The string is just a representation of the DOM structure inside that element. 该字符串只是该元素内部DOM结构的表示。 So once you've passed your string to innerHTML, it converts it to actual DOM elements. 因此,一旦将字符串传递给innerHTML,它将把它转换成实际的DOM元素。 Retrieving innerHTML asks the browser to do the opposite; 检索innerHTML要求浏览器执行相反的操作; Converting those nodes back to a string however it sees fit. 可以将那些节点转换回字符串。 In short, innerHTML is not a variable, it's a function and it isn't actually "storing" anything. 简而言之,innerHTML并不是变量,它是一个函数,实际上并没有“存储”任何东西。

If you want to store an exact copy of something, don't use HTML, it's strictly for display. 如果您想存储某物的精确副本,请不要使用HTML,它只能用于显示。 Instead, store the variables in javascript and associate the id of each element with your variables to do the comparisons. 而是将变量存储在javascript中,并将每个元素的id与您的变量关联以进行比较。 You can then look up those elements by the ID and apply classes to them to trigger animations. 然后,您可以通过ID查找这些元素,并将类应用于它们以触发动画。

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

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