【发布时间】:2017-12-02 06:19:35
【问题描述】:
当我在 Jekyll 中写博客文章时,我很恼火的是,默认情况下,我使用降价创建的文章链接会在同一个窗口中打开。所以它让我的读者远离了我的网站。
是否有任何方法,使用 HTML、CSS、Liquid 或任何其他方式在帖子布局中默认创建链接 target="_blank"?
【问题讨论】:
标签: jquery html css jekyll liquid
当我在 Jekyll 中写博客文章时,我很恼火的是,默认情况下,我使用降价创建的文章链接会在同一个窗口中打开。所以它让我的读者远离了我的网站。
是否有任何方法,使用 HTML、CSS、Liquid 或任何其他方式在帖子布局中默认创建链接 target="_blank"?
【问题讨论】:
标签: jquery html css jekyll liquid
我了解到您想在帖子中插入外部链接,这些链接将在新标签页中打开。
该行为需要将target="_blank" 属性添加到链接元素,因此浏览器知道执行该行为。
至少在更流行的 jekyll markdown 口味之一的 Kramdown 中,我相信可以简单地做到以下几点:
[achorText](url){:target="_blank"}
希望这会有所帮助。
【讨论】:
您可以使用以下代码为所有 s 标签添加 attr 或专门发布标签
$(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找到的。
【讨论】: