简体   繁体   English

如何获取html页面的源代码

[英]How to get source code of html page

This is a link for an example of Edit source Code:这是编辑源代码示例的链接:

http://neokoenig.github.io/jQuery-gridmanager/demo/tinymce.html http://neokoenig.github.io/jQuery-gridmanager/demo/tinymce.html

the button which value is </> .值为</>的按钮。

He get the code HTML even if he changes the content of the grid.即使他更改了网格的内容,他也会得到代码 HTML。

How to get the source code HTML using JavaScript or jQuery?如何使用 JavaScript 或 jQuery 获取源代码 HTML?

Thanks.谢谢。

you can use the html method with jquery, ie for get the whole page html like this: 您可以将html方法与jquery一起使用,例如,使用以下方法获取整个页面的html:

$( document ).ready(function() {
    console.log($("html").html());
})

I'm not sure what you want to do exactly but if you want to get raw source code from jQuery you can use the following: 我不确定您到底想做什么,但是如果您想从jQuery获取原始代码,则可以使用以下代码:

var html = $('element').html();

And with pure javascript from an id 并从ID使用纯JavaScript

var html = document.getElementById('id').innerHTML;

or from classname 或从类名

var html = document.getElementsByClassName('class').innerHTML;

And to get the content of your example (which is an editor called tinymce) you can use the command tinymce.activeEditor.getContent(); 为了获得示例的内容(一个名为tinymce的编辑器),可以使用命令tinymce.activeEditor.getContent(); or tinyMCE.get('myTextarea').getContent() tinyMCE.get('myTextarea').getContent()


EDIT: If you want to listen for changes with jQuery and display to html dynamically you'd want to do something like this: 编辑:如果您想用jQuery侦听更改并动态显示为html,则需要执行以下操作:

$('yourTextArea').keyup(function() {
    var html = $(this).val();
    $('yourElementToDisplayTheHTML').html(html);
});

Using JavaScript 使用JavaScript

document.documentElement.outerHTML

More info 更多信息

or 要么

document.documentElement.innerHTML

More info 更多信息

I've built a simple API, just use this:我已经构建了一个简单的 API,只需使用这个:

<script src="https://cdn.jsdelivr.net/gh/Parking-Master/viewsource@latest/vs.js"></script>

In your JavaScript:在你的 JavaScript 中:

getSource('https://example.com/');

in http response header, you can find "content-type" header. 在http响应标头中,您可以找到“ content-type”标头。 that header indicates how contents of page should be rendered. 该标题指示页面内容应如何呈现。 so, if you want to render page as plain text("source code", as you mentioned), just change that header like this -> "content-type : text/plain; charset=UTF-8" 因此,如果要将页面呈现为纯文本格式(如您提到的那样,则为“源代码”),则只需更改标题,例如->“ content-type:text / plain; charset = UTF-8”

if you can use ajax in jquery or javascript 如果您可以在jquery或javascript中使用ajax

use like this 这样使用

$("button").click(function(){
$.ajax({url: "page_you_want.html", success: function(result){
    alert(result);
}});

}); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM