如何使用 CSS 或 Multimarkdown 在新选项卡中打开超链接?
我正在使用 Text::MultiMarkdown 从 MultiMarkdown 文档。
我希望所有链接都在新选项卡中打开。
有没有办法使用 CSS 模板或直接在 MultiMarkdown 文档中配置此行为(无需在 MultiMarkdown 文档中的每个链接周围显式编写 HTML)?
I am using Text::MultiMarkdown to create HTML files from MultiMarkdown documents.
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)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
CSS 中绝对不是这样——CSS 只关心元素的显示方式,而不关心它们的行为方式。
应该可以将
添加到 HTML 文档的头部(使用 XSLT),但这与将其添加到每个链接相同。Definitely not in CSS - that is only concerned with the way the elements appear, not how they behave.
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.在 HTML 和/或 JavaScript 中,您只能初始化新窗口的打开。用户在某些 UA 中能够强制将新窗口作为新选项卡打开。但你无法控制这种行为。
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.
理论上,您可以使用 CSS3 来做到这一点: http://www.w3.org/TR/ css3-hyperlinks/ - 然而没有常见的浏览器实现过这个。原因可能是,人们普遍认为何时打开新窗口或选项卡的选择应由用户单独决定。
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.
您无法在 CSS 中执行此操作,但可以使用源代码。
您可以子类化
Text::MultiMarkdown
并提供您自己的_GenerateAnchor
实现,与此类似的东西可能会起作用:这有点像
_GenerateAnchor
不是公共接口的一部分。您还需要使用 OO 接口而不仅仅是markdown
函数。您还可以联系
Text::MultiMarkdown
作者,看看他是否会为此类事情添加一个标志。也许您可以提供一个补丁来开始工作。您还可以使用
HTML::Parser
及其朋友来解析来自Text::MultiMarkdown
的 HTML 并自行添加target
属性。You can't do this in CSS but you can use the source.
You could subclass
Text::MultiMarkdown
and provide your own implementation of_GenerateAnchor
, something similar to this might work:This is a bit kludgey as
_GenerateAnchor
isn't part of the public interface. You'd also need to use the OO interface rather than just themarkdown
function.You could also contact the
Text::MultiMarkdown
author and see if he'll add a flag for this sort of thing. Maybe you could provide a patch to get things started.You can also use
HTML::Parser
and friends to parse the HTML that comes out ofText::MultiMarkdown
and add thetarget
attributes yourself.