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

上述代码摘录:


//代码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 @ 2019-04-27 17:11  Gitwow  阅读(2824)  评论(0编辑  收藏  举报