Javascript 显示 ajax、Jquery 返回的响应的 html 代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8900875/
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
display html code of response returned by ajax, Jquery
提问by user1038814
I have a jquery AJAX function which retrieves some HTML markup and displays it on the page. I would also like to display the html code of this HTML returned. I've looked around for a solution but not finding any. Can someone please help. Many thanks
我有一个 jquery AJAX 函数,它检索一些 HTML 标记并将其显示在页面上。我还想显示返回的这个 HTML 的 html 代码。我四处寻找解决方案,但没有找到任何解决方案。有人可以帮忙吗。非常感谢
$.post('get_news.php', $("#gifForm").serialize(), function(data) {
//Show HTML
$('#output').html(data);
//Show HTML code
$('#output_code').html(data);
});
回答by Hoppy
try using the text()function. This will escape and display html code. http://api.jquery.com/text/
尝试使用text()函数。这将转义并显示 html 代码。http://api.jquery.com/text/
$.post('get_news.php', $("#gifForm").serialize(), function(data) {
//Show HTML
$('#output').html(data);
//Show HTML code
$('#output_code').text(data);
});
回答by Troy Barlow
You can surround your html with "code" and it should display it as is without rendering the html:
你可以用“代码”包围你的 html,它应该按原样显示它而不渲染 html:
$('#output_code').html("<code>" + data + "</code>");
回答by adeneo
From your code it looks like this is sent back from a PHP file, if so you could just use one of PHP's htmlentities functions to convert the result before sending it back to the Ajax function, and it will display just fine, preferrably in a html pre tag.
从您的代码看来,这是从 PHP 文件发回的,如果是这样,您可以在将结果发送回 Ajax 函数之前使用 PHP 的 htmlentities 函数之一来转换结果,并且它会显示得很好,最好是在 html 中预标签。
You should also use jQuery text() and not html().
您还应该使用 jQuery text() 而不是 html()。
If you would like syntax highlighting, line numbers and lots of other stuff, GeSHiand Syntax Highlighteris used by a lot of sites to display code snippets.
如果您想要语法高亮、行号和许多其他东西,很多站点都使用GeSHi和Syntax Highlighter来显示代码片段。
回答by Diodeus - James MacFarlane
Put the output in a textarea. It will maintain the formatting. (if the HTML contains a textarea, you'll have to escape it first).
将输出放在 textarea 中。它将保持格式。(如果 HTML 包含文本区域,则必须先对其进行转义)。
回答by ShankarSangoli
You can try to use escape on the HTML data. Try this
您可以尝试对 HTML 数据使用转义。尝试这个
$.post('get_news.php', $("#gifForm").serialize(), function(data) {
//Show HTML
$('#output').html(data);
//Show HTML code
$('#output_code').html(escape(data));
});
回答by Silvertiger
The code returned and displayed by the AJAX is anything created and output by the PHP script you are calling. All of the code should be present so you may need to adjust the output of the PHP to include the elements you want.
AJAX 返回和显示的代码是您正在调用的 PHP 脚本创建和输出的任何内容。所有代码都应该存在,因此您可能需要调整 PHP 的输出以包含您想要的元素。
What does the PHP page do? database work? query and display results? what you should see in the AJAX results is the same thing you would get if you tried the php script on its own.
PHP 页面有什么作用?数据库工作?查询和显示结果?如果您单独尝试 php 脚本,您应该在 AJAX 结果中看到的结果与您得到的结果相同。