有没有办法打开所有<a href>新窗口中页面上的链接? [英] Is there a way to open all <a href> links on a page in new windows?
问题描述
我有一堆 <a href=".html">
链接,我想让它们都在新窗口中打开.
I have a bunch of <a href=".html">
links and want to make them all open in new windows.
我知道我可以进行搜索并全部替换以将 target="_blank"
添加到我所有的 <a href="...">
链接.
I know I can do a search and replace all to add target="_blank"
to all my <a href="...">
links.
但是,有没有一种快速的方法,比如设置一个 CSS 样式,或者添加一些 JavaScript 代码,来完成同样的事情?
However, is there a quick way, such as setting a CSS style, or adding some JavaScript code, to accomplish the same thing?
推荐答案
如果您的页面仅包含链接,请考虑
.这会在新窗口中打开每个链接(但也包括表单的目标,除非被 <form target="_self">
覆盖.
If you have a page consisting of only links, consider <base target="_blank">
. This opens every link in a new window (but also includes the targets of forms, unless overridden with <form target="_self">
.
正如其他人所展示的,在不修改 HTML 源代码的情况下,您可以使用 Javascript 来遍历所有 标记并添加
target
属性 或添加设置目标属性的 事件侦听器动态.
As others have shown, without modifying the HTML source, you can use Javascript to iterate through all <a>
tags and add the target
attribute or add an event listener that sets the target attribute dynamically.
这篇关于有没有办法打开所有<a href>新窗口中页面上的链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!