使用JavaScript获取未解析的(原始)HTML [英] Getting unparsed (raw) HTML with JavaScript

查看:96
本文介绍了使用JavaScript获取未解析的(原始)HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取网页中元素的实际html代码.

I need to get the actual html code of an element in a web page.

例如,如果元素内的实际html代码为"How to fix"

For example if the actual html code inside the element is "How to fix"

运行此JavaScript:

Running this JavaScript:

getElementById('myE').innerHTML

给我"How to fix",它是已解析的HTML.

Gives me "How to fix" which is the parsed HTML.

如何使用JavaScript获取未解析的"How to fix"?

How can I get the unparsed "How to fix" using JavaScript?

推荐答案

您应该使用的工具:

元素测试:

<div id="myE">How to&nbsp;fix</div>​

JavaScript测试:

JavaScript test:

alert(document.getElementById("myE​​​​​​​​").innerHTML); //alerts "How to&nbsp;fix"

您可以在这里尝试.确保无论您在哪里使用 ,结果都不会将&nbsp;显示为空格,这很可能是这种情况.如果要将其显示在专为HTML设计的位置,则需要对其进行转义.

You can try it out here. Make sure that wherever you're using the result isn't show &nbsp; as a space, which is likely the case. If you want to show it somewhere that's designed for HTML, you'll need to escape it.

这篇关于使用JavaScript获取未解析的(原始)HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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