簡體   English   中英

是否可以一次更改網站上的所有錨標簽屬性?

[英]Is it possible to change all anchor tags properties on a website at once?

我最近了解到target="_blank"很容易受到攻擊,我們必須使用rel="noopener" 我正在開發一個網站項目,其中所有錨標簽都使用目標屬性。

只需使用以下命令就可以一次更改這些anchor texts的 colors:

a {color: blue;}

在網站的頭部。

但如果我嘗試做

a {
rel="noopener"
target ="_blank"
}

上面的代碼沒有任何作用。 因為 rel 和target不在CSS中。

那么有人如何為整個站點設置這些attributes呢?

另外,我嘗試在 w3schools 上進行搜索,但他們在 CSS 中沒有任何答案。

你將需要 JS。

document.querySelectorAll("a").forEach(a => {
   a.setAttribute("rel", "noopener");
   a.setAttribute("target", "_blank");
});

我個人會為此使用 JS。 css: https://developer.mozilla.org/en-US/docs/Web/CSS/attr js: httpsA///en -orgattributes//developer.mozilla//

使用 Vanilla JS 查找元素。 循環然后更改屬性。

document.querySelectorAll('a[target="_blank"]').forEach(function(el){
    el.setAttribute('rel', 'noopener');
});

然后申請 CSS

a[target ="_blank"][rel="noopener"] {
    color: blue;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM