Jekyll Github Page的链接在新页面中显示?

时间:2017-12-02 06:19:36

标签: jquery html css jekyll liquid

当我在Jekyll写博客文章时,我很恼火,默认情况下,我在同一个窗口中打开了使用降价打开的帖子链接。所以它让我的读者远离我的网站。

https://cecilialee.github.io/

是否有任何方法,默认使用HTML,CSS,Liquid或任何其他方式在帖子布局target="_blank"中制作链接?

谢谢!

3 个答案:

答案 0 :(得分:3)

我们了解您要在帖子中插入外部链接,这些链接会在新标签页中打开。

这种行为需要将target="_blank"属性添加到link元素,因此浏览器知道要执行该行为。

至少在Kramdown中,一种比较流行的jekyll降价口味,我相信可以简单地完成如下:

[achorText](url){:target="_blank"}

希望这有帮助。

答案 1 :(得分:2)

您可以使用以下代码为所有s标签添加attr或专门发布标签

$(document).ready(function(){
  $('#link_other a').attr('target', '_blank');
});

答案 2 :(得分:0)

如@JoostS所述,还有一个没有jQuery的选项:

创建一个新文件_includes/new-window-fix.html,其中包含:

<script>
    //open external links in a new window
    function external_new_window() {
        for(var c = document.getElementsByTagName("a"), a = 0;a < c.length;a++) {
            var b = c[a];
            if(b.getAttribute("href") && b.hostname !== location.hostname) {
                b.target = "_blank";
                b.rel = "noopener";
            }
        }
    }
    //open PDF links in a new window
    function pdf_new_window ()
    {
        if (!document.getElementsByTagName) return false;
        var links = document.getElementsByTagName("a");
        for (var eleLink=0; eleLink < links.length; eleLink ++) {
        if ((links[eleLink].href.indexOf('.pdf') !== -1)||(links[eleLink].href.indexOf('.doc') !== -1)||(links[eleLink].href.indexOf('.docx') !== -1)) {
            links[eleLink].onclick =
            function() {
                window.open(this.href);
                return false;
            }
        }
        }
    } 
    pdf_new_window();
    external_new_window();
</script>

然后,修改布局文档的底部以包含此新文件:

...
{% include new-window-fix.html %}
</body>
</html>

此解决方案在jekyllcodex.org上找到。