用HTML解开HTML元素的原始文本内容 [英] Getting raw text content of HTML element with HTML uninterpreted

查看:90
本文介绍了用HTML解开HTML元素的原始文本内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了我的大脑,无法弄清楚如何完成这项工作。这是我想要做的:

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向导我的理智!

解决方案

引用为您提供被解析以生成
节点的原始HTML源代码,因为它不再拥有该信息,而是从存储在数据库中的数据生成
新HTML DOM。浏览器决定
如何格式化HTML序列化;不同的浏览器产生
不同的HTML,并且它可能与你格式化
不同

总结: innerHTML / innerText / text / textContent / nodeValue / indexOf ,它们都不会给你未解析的文本。



唯一可行的方法是使用正则表达式,或者你可以在页面上做一个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:

<div id=derp>&quot;Hi, my name is..&quot;</div>

Javascript:

var div = document.getElementById('derp');
alert(div.innerHTML);
alert(div.innerText);
alert(div.textContent);

All of those alerts interpret and return the &quot; as " in the resulting string. I want to get the raw text with &quot; uninterpreted.

They all return:

"Hi, my name is.."

When I want to get:

&quot;Hi, my name is..&quot;

Is there a way to do this? Preferably without trying to use a regex to replace every instance of " with &quot;.

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 " would be a headache to implement because of other regex matching/parsing that needs to occur.

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: innerHTML/innerText/text/textContent/nodeValue/indexOf, none of them will give you the unparsed text.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆