问题描述
我最近了解到target="_blank"
是易受攻击的,我们必须使用rel="noopener"
.我正在一个网站项目中,所有锚标签都使用target属性.
I have recently learned that target="_blank"
is vulnerable and we have to use rel="noopener"
. I am working on a website project where all anchor tags are using the target attribute.
只需使用以下一项即可更改这些anchor texts
的颜色:
It is possible to change the colors of these anchor texts
at once just by using:
a {color: blue;}
在网站顶部.
但是如果我尝试做
a {
rel="noopener"
target ="_blank"
}
上面的代码没有任何作用.因为rel和target
未包含在CSS
中.
The above code does not have any effect. Because rel and target
are not covered in CSS
.
那么有人怎么为整个站点设置这些attributes
?
So how could someone set these attributes
for the entire site?
此外,我尝试在w3schools上进行搜索,但是在CSS中他们对此没有任何答案.
Also, I tried searching on w3schools but they don't have any answer to it in CSS.
推荐答案
使用Vanilla JS查找元素.循环然后更改属性.
Find elements using Vanilla JS. Loop then change attributes.
document.querySelectorAll('a[target="_blank"]').forEach(function(el){
el.setAttribute('rel', 'noopener');
});
然后应用CSS
a[target ="_blank"][rel="noopener"] {
color: blue;
}
这篇关于是否可以一次更改网站上的所有定位标记属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!