简体   繁体   English

如何更改页面中所有用户个人资料链接的颜色?

[英]How to change the color of all user profile links in a page?

I'm trying to change the color of every user's profile link in my browser using a userscript.我正在尝试使用用户脚本更改浏览器中每个用户个人资料链接的颜色。 My code:我的代码:

// ==UserScript==
// @name         Color changer
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  none
// @author       You
// @match        https://puzzling.stackexchange.com/questions/*
// @grant        none
// ==/UserScript==

let users = document.getElementsByClassName("user-details");

for (let user of users) {
    user.getElementsByTagName("a")[0].style.color = "red";
}

For some reason,因为某些原因,

  • for some posts this only changes the color of the OP's link in every question page, not the links of the commenters and answerers.对于某些帖子,这只会更改每个问题页面中 OP 链接的颜色,而不是评论者和回答者的链接。

Error:错误:

ERROR: Execution of script 'New Userscript' failed! user.getElementsByTagName(...)[0] is undefined
  • for some posts this doesn't work at all.对于某些帖子,这根本不起作用。

Error:错误:

ERROR: Execution of script 'New Userscript' failed! user.getElementsByTagName(...)[0] is undefined
  • for some posts this only changes the color of the OP's link and answer's link in every question page, not the links of the commenters.对于某些帖子,这只会更改每个问题页面中 OP 链接和答案链接的颜色,而不是评论者的链接。

No error.没有错误。


How can I change the color of all the user profile links in the page?如何更改页面中所有用户个人资料链接的颜色?

Try this:尝试这个:

document.querySelectorAll('.user-details a').forEach( item => item.style.color = "red")

You can make more complex CSS selectors with "querySelectorAll".您可以使用“querySelectorAll”制作更复杂的 CSS 选择器。

The user comments have a different CSS class attached to them so you would then need to :用户评论附加了不同的 CSS 类,因此您需要:

document.querySelectorAll('.comment-user').forEach( item => item.style.color = "red")

OR ... If you want to do both you can try separating multiple CSS queries with a comma:或者...如果你想同时做,你可以尝试用逗号分隔多个 CSS 查询:

document.querySelectorAll('.user-details a, .comment-user').forEach( item => item.style.color = "red")

Generally, you should NOT be using .getElementsByClassName() or .getElementsByTagName() in 2020 and especially not in a loop. 通常,您不应该在 2020 年使用.getElementsByClassName().getElementsByTagName() ,尤其是不要在循环中使用。 As my other post gets into, these are some of the earliest DOM querying methods and they return "live" node lists, which can be costly, especially when used in conjunctions with a loop.正如我的另一篇文章所介绍的那样,这些是一些最早的 DOM 查询方法,它们返回“实时”节点列表,这可能很昂贵,尤其是与循环结合使用时。 .querySelectorAll() is the modern alternative and is what you really should be using. .querySelectorAll()是现代替代方案,是您真正应该使用的。

You just need to properly query for all the <a> descendants of any user-details element and loop through the results, adding a new class to use for each one (you also should be avoiding inline styles when you can as they lead to duplication of code, don't scale well, and are hard to override).您只需要正确查询任何user-details元素的所有<a>后代并遍历结果,为每个元素添加一个新类(您也应该尽可能避免内联样式,因为它们会导致重复代码,不能很好地扩展,并且很难覆盖)。 Instead, use CSS classes, which are easy to add and remove.相反,请使用易于添加和删除的 CSS 类。

document.querySelector(".user-details a").forEach(function(item){
  item.classList.add("newColor");
});

And, of course, make sure you have the newColor class defined in your CSS:而且,当然,请确保您在 CSS 中定义了newColor类:

.newColor { color: someColorYouWant; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM