如何在 CSS 中向 <a> 添加属性

How to add properties to <a> in CSS

对于我网站上的每个 <a> 标签,我都必须添加属性 "target="_blank" rel="noopener noreferrer"。但是,当我将其放入我的 CSS 样式表时它不起作用:

a {
target: "_blank";
rel: "noopener noreferrer";
}

它说这些属性是未知的,这没有意义,因为当我将它们放入实际标签中时,它们会被识别并起作用。

Only styling can be added with css not html attributes. Eg
 You can replace below inline css style with external css
     <a href="#" style="color:blue">

    to
    <a href="#">
     a{
        color: blue;
        }

  Whatever is there in style attribute inline can be replaced with css. 
         <a href="#" target="_blank"> 
  This is target attribute and it cannot be replaced using css. Only 
   inline css using style can be replaced with external css.

您可以尝试使用 JavaScript。

<script>
    var links = document.getElementsByTagName('a');
    
        for (var i = links.length - 1; i >= 0; --i) {
            links[i].setAttribute("target", "_blank");
            links[i].setAttribute("rel", "noopener noreferrer");
        }
</script>

将此插入包含链接的 html 文档的底部。或者您可以将其另存为文件并将脚本包含在 HTML 文档的 header 中。