显示HTML的源代码 [英] Displaying Source Code of HTML
问题描述
我正在为网站创建功能索引,我认为如果用户只需单击链接即可查看源代码,而不使用浏览器开发人员工具,那就太好了.经过研究,我发现我们可以轻松地查看当前页面的源代码,如下所示:
I'm creating a Feature Index for a website and I thought that it would be nice if the user could just click on a link to view the source code instead of using the browser developer tools. After some research I found that we can easily view the source code of the current page like this:
<a onClick='window.location="view-source:" + window.location.href'>View Source</a>
可悲的是,我仍然是一个很大的javascript新手,并且想知道是否有一种方法可以超链接到网站上其他HTML页面的源代码.
Sadly, i'm kind of a big javascript noob still and was wondering if there is a way to hyperlink to the source code of other HTML pages on the site.
谢谢
推荐答案
是的,与您现在完全一样:使用 view-source:
将该用户引导至该URL.给出的示例 view-source:http://www.stackoverflow.com
会将您的访问者定向到stackoverflow的源.但是请注意,这完全取决于您使用的浏览器,这意味着:某些用户会看到源,而其他用户则不会.
Yeah, exactly the same as you did now: direct the user to the url with view-source:
prepended to the url. Example given, view-source:http://www.stackoverflow.com
will direct your visitor to the source of stackoverflow. But beware that this depends solely on the browser you are using, meaning: some users will see the source, other users might not.
还有一个额外的jquery来转换所有源代码链接(带有"source"类)(未经测试)
And a bonus jquery to convert all source code links (with class 'source') (not tested)
<a href="page1.html" class="source">Page 1 (source)</a>
<a href="page2.html" class="source">Page 2 (source)</a>
<a href=http://www.external.com" class="source">External (source)</a>
jQuery(document).ready(function ($) {
$('a.source').each(function() {
$(this).attr('href', 'view-source:' + $(this).attr('href'));
});
});
这篇关于显示HTML的源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!