markdown生成的a标签如何在新页面打开

原始的超链接语法这样写:[超链接的名字](url)
在新窗口中打开:[超链接的名字](url?_blank)
在本窗口中打开:[超链接的名字](url?_self)默认是在本窗口中打开

但上面的说法貌似不行,再往下看

MarkDown 超链接页面内和通过新窗口打开 - jingbin_的博客 - CSDN博客

Markdown语法:在新窗口新标签页中打开 – 小独裁者的国度

markdown生成的a标签如何在新页面打开 - Feng_Yu的回答 - SegmentFault 思否
html - Can I create links with 'target="_blank"' in Markdown? - Stack Overflow

上述代码摘录:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//代码1
var links = document.links;
 
for (var i = 0, linksLength = links.length; i < linksLength; i++) {
   if (links[i].hostname != window.location.hostname) {
       links[i].target = '_blank';
   }
}
 
//代码2
$(document.links).filter(function() {
    return this.hostname != window.location.hostname;
}).attr('target', '_blank');
 
//代码3
var pattern = /a href=/g;
var sanitizedMarkDownText = rawMarkDownText.replace(pattern,"a target='_blank' href=");
 
//代码4
var links = document.querySelectorAll( '.post-content a' ); 
for (var i = 0, length = links.length; i < length; i++) { 
    if (links[i].hostname != window.location.hostname) {
        links[i].target = '_blank';
    }
}
 
//代码5
<script type="text/javascript" charset="utf-8">
  // Creating custom :external selector
  $.expr[':'].external = function(obj){
      return !obj.href.match(/^mailto\:/)
              && (obj.hostname != location.hostname);
  };
 
  $(function(){
    // Add 'external' CSS class to all external links
    $('a:external').addClass('external');
 
    // turn target into target=_blank for elements w external class
    $(".external").attr('target','_blank');
 
  })
</script>
 
//代码6
/*
 * For all links in the current page...
 */
$(document.links).filter(function() {
    /*
     * ...keep them without `target` already setted...
     */
    return !this.target;
}).filter(function() {
    /*
     * ...and keep them are not on current domain...
     */
    return this.hostname !== window.location.hostname ||
        /*
         * ...or are not a web file (.pdf, .jpg, .png, .js, .mp4, etc.).
         */
        /\.(?!html?|php3?|aspx?)([a-z]{0,3}|[a-zt]{0,4})$/.test(this.pathname);
/*
 * For all link kept, add the `target="_blank"` attribute.
 */
}).attr('target', '_blank');
 
//代码7
var links = document.links;
for (var i = 0; i < links.length; i++) {
    if (!links[i].target) {
        if (
            links[i].hostname !== window.location.hostname ||
            /\.(?!html?)([a-z]{0,3}|[a-zt]{0,4})$/.test(links[i].pathname)
        ) {
            links[i].target = '_blank';
        }
    }
}
posted @   Gitwow  阅读(3451)  评论(0编辑  收藏  举报
编辑推荐:
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
阅读排行:
· 《HelloGitHub》第 108 期
· Windows桌面应用自动更新解决方案SharpUpdater5发布
· 我的家庭实验室服务器集群硬件清单
· C# 13 中的新增功能实操
· Supergateway:MCP服务器的远程调试与集成工具

点击右上角即可分享
微信分享提示