使用JavaScript获取未解析的(原始)HTML [英] Getting unparsed (raw) HTML with JavaScript
问题描述
我需要获取网页中元素的实际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 fix</div>
JavaScript测试:
JavaScript test:
alert(document.getElementById("myE").innerHTML); //alerts "How to fix"
您可以在这里尝试.确保无论您在哪里使用 ,结果都不会将
显示为空格,这很可能是这种情况.如果要将其显示在专为HTML设计的位置,则需要对其进行转义.
You can try it out here. Make sure that wherever you're using the result isn't show
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屋!