用HTML解开HTML元素的原始文本内容 [英] Getting raw text content of HTML element with HTML uninterpreted
问题描述
我已经搜索了我的大脑,无法弄清楚如何完成这项工作。这是我想要做的:
HTML:
< div id = derp>& quot;我的名字是..& quot;< / div>
Javascript:
var div = document.getElementById('derp');
alert(div.innerHTML);
alert(div.innerText);
alert(div.textContent);
所有这些警报解释并返回& quot;
作为结果字符串中的。我希望获得未加解释的
& quot;
的原始文本。
它们全部返回:
我的名字是..
当我想获得时:
& quot;我的名字是......& quot;
有没有办法做到这一点?最好不要试图用正则表达式来替换的所有实例
与&安培; QUOT;
。
这是我想要做的事情的一个很长的故事,但简单地使用replace()来搜索并替换的每个实例
感谢任何可以保存的Javascript向导我的理智!
引用为您提供被解析以生成 总结: 唯一可行的方法是使用正则表达式,或者你可以在页面上做一个ajax post ,但这是一个不好的做法。 I have Googled my brains out and can't figure out how to make this work. Here is what I'm trying to do: HTML: Javascript: All of those alerts interpret and return the They all return: When I want to get: Is there a way to do this? Preferably without trying to use a regex to replace every instance of It's kind of a long story of what I'm trying to do, but simply using replace() to search and replace every instance of Thanks in advance for any Javascript wizards who can save my sanity! To quote bobince When you ask the browser for an element node's innerHTML, it doesn't
give you the original HTML source that was parsed to produce that
node, because it no longer has that information. Instead, it generates
new HTML from the data stored in the DOM. The browser decides on how
to format that HTML serialisation; different browsers produce
different HTML, and chances are it won't be the same way you formatted
it originally. In summary: The only possible way to do this is with regex, or you can do an ajax post to the page itself, but that is a bad practice. 这篇关于用HTML解开HTML元素的原始文本内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
节点的原始HTML源代码,因为它不再拥有该信息,而是从存储在数据库中的数据生成
新HTML DOM。浏览器决定
如何格式化HTML序列化;不同的浏览器产生
不同的HTML,并且它可能与你格式化
不同
innerHTML / innerText / text / textContent / nodeValue / indexOf
,它们都不会给你未解析的文本。
<div id=derp>"Hi, my name is.."</div>
var div = document.getElementById('derp');
alert(div.innerHTML);
alert(div.innerText);
alert(div.textContent);
"
as "
in the resulting string. I want to get the raw text with "
uninterpreted."Hi, my name is.."
"Hi, my name is.."
"
with "
. "
would be a headache to implement because of other regex matching/parsing that needs to occur.
innerHTML/innerText/text/textContent/nodeValue/indexOf
, none of them will give you the unparsed text.