当我使用Jekyll写博客文章时,我很烦恼默认情况下用Markdown创建的博客链接在同一窗口中打开。所以它让我的读者离开了我的网站。
有没有任何方法,使用HTML、CSS、Liquid或其他方式使文章布局中的链接默认为
有没有任何方法,使用HTML、CSS、Liquid或其他方式使文章布局中的链接默认为
target="_blank"
?
我明白您想在文章中插入外部链接,且希望链接在新标签页中打开。
这种行为需要在链接元素中添加target="_blank"
属性,以便浏览器知道执行该行为。
至少在Kramdown中,这是一种比较流行的Jekyll markdown风格,我认为可以简单地按照以下方式完成:
[achorText](url){:target="_blank"}
$(document).ready(function(){
$('#link_other a').attr('target', '_blank');
});
正如@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上找到的。