Javascript 从网站获取正文标签值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8972075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
get body tag value from website
提问by Tanu Garg
I need to fetch body tag from the url which i open. I am using following code but it is not working. Please suggest.
我需要从我打开的 url 中获取 body 标签。我正在使用以下代码,但它不起作用。请建议。
document.getElementsByTagName('body')[0].innerHTML;
Thanks Tanu
谢谢塔努
回答by Umesh Patil
Copy below code in url navigation bar. See, whole body content alerted.
在 url 导航栏中复制以下代码。看,全身内容被提醒。
javascript:(alert(document.body.innerHTML));
Its means you can get the body content by just body.innerHTML. you don't require to use getElementsByTagName.
这意味着您可以通过 body.innerHTML 获取正文内容。您不需要使用 getElementsByTagName。
回答by Luminously
Did you try document.body.innerHTML?
你试过 document.body.innerHTML 吗?
回答by Sujoy
Here is my simple test
这是我的简单测试
<html>
<head>
<title>TEST </title>
<script type="text/javascript">
MyFunction = function () {
alert(document.body.innerHTML);
};
</script>
</head>
<body onload="MyFunction();">
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
</html>
it's working on FF
它正在 FF 上工作
回答by Quentin
Assuming that:
假如说:
- The script runs after the document has loaded
- The script is running on the page that you want the body content from (which would notinclude the address bar in Firefox).
- 脚本在文档加载后运行
- 该脚本正在您想要正文内容的页面上运行(不包括Firefox 中的地址栏)。
That that will work, although it is uglier than document.body.innerHTML
.
那会起作用,尽管它比document.body.innerHTML
.