Html 如何使用 CSS 或 Multimarkdown 在新选项卡中打开超链接?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4287156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 05:28:18  来源:igfitidea点击:

How can I make hyperlinks open in a new tab using CSS or Multimarkdown?

htmlcssperlhyperlinkmarkdown

提问by David B

I am using Text::MultiMarkdownto create HTML files from MultiMarkdowndocuments.

我正在使用Text::MultiMarkdownMultiMarkdown文档创建 HTML 文件。

I would like all links to open in a new tab.

我希望所有链接都在新选项卡中打开。

Is there a way to configure this behavior using a CSS template, or directly in the MultiMarkdown document (without explicitly writing HTML around each link in the MultiMarkdown document)?

有没有办法使用 CSS 模板或直接在 MultiMarkdown 文档中配置此行为(无需在 MultiMarkdown 文档中的每个链接周围明确编写 HTML)?

回答by Piskvor left the building

Definitely not in CSS - that is only concerned with the way the elements appear, not how they behave.

绝对不是在 CSS 中——它只关心元素的显示方式,而不是它们的行为方式。

It should be possible to add <base target="_blank">to the head of the HTML document (using XSLT), but that's on par with adding it to each link.

应该可以添加<base target="_blank">到 HTML 文档的头部(使用 XSLT),但这与将它添加到每个链接是一样的。

回答by anddoutoi

In HTML and/or JavaScript you can only initialize the opening of a new window. The user is in some UAs able to force the opening of a new window as a new tab instead. But you can not control this behaviour.

在 HTML 和/或 JavaScript 中,您只能初始化新窗口的打开。用户在某些 UA 中能够强制打开新窗口作为新选项卡。但是您无法控制这种行为。

回答by Gerald Senarclens de Grancy

In theory, you could do this with CSS3: http://www.w3.org/TR/css3-hyperlinks/- however no common browser ever implemented this. The reason might be that it is a common believe that the choice of when a new window or tab is opened should be left to the user alone.

理论上,你可以用 CSS3 做到这一点:http: //www.w3.org/TR/css3-hyperlinks/- 但是没有一个普通的浏览器实现过这个。原因可能是人们普遍认为何时打开新窗口或选项卡应该由用户自己决定。

回答by mu is too short

You can't do this in CSS but you can use the source.

你不能在 CSS 中做到这一点,但你可以使用源代码。

You could subclass Text::MultiMarkdownand provide your own implementation of _GenerateAnchor, something similar to this might work:

您可以子类化Text::MultiMarkdown并提供您自己的 实现_GenerateAnchor,类似的东西可能会起作用:

sub _GenerateAnchor {
    my ($self, $whole_match, $link_text, $link_id, $url, $title, $attributes) = @_;
    if($url
    && index($url, '#') != 0) {
        $attributes = $attributes ? $attributes . ' target="_blank"' : 'target="_blank"';
    }
    return $self->SUPER::_GenerateAnchor($whole_match, $link_text, $link_id, $url, $title, $attributes);
}

This is a bit kludgey as _GenerateAnchorisn't part of the public interface. You'd also need to use the OO interface rather than just the markdownfunction.

这有点笨拙,因为_GenerateAnchor它不是公共界面的一部分。您还需要使用 OO 接口而不仅仅是markdown函数。

You could also contact the Text::MultiMarkdownauthor and see if he'll add a flag for this sort of thing. Maybe you could provide a patch to get things started.

你也可以联系Text::MultiMarkdown作者,看看他是否会为这种事情添加一个标志。也许你可以提供一个补丁来开始工作。

You can also use HTML::Parserand friends to parse the HTML that comes out of Text::MultiMarkdownand add the targetattributes yourself.

您还可以使用HTML::Parser和朋友来解析出来的 HTMLText::MultiMarkdowntarget自己添加属性。