我收到类型错误,因为脚本无法读取 null 的属性“style”。
我尝试将属性附加到变量,认为它会起作用
// ==UserScript==
// @name New Script
// @namespace Violentmonkey Scripts
// @match https://simply-how.com/enhance-and-fine-tune-any-web-page-the-complete-user-scripts-guide
// @grant none
// ==/UserScript==
var pTag = document.getElementById("p");
pTag.style.color = "#4040c9"
I expect for all the "p" tags to change to the color #4040c9
最佳答案
使用 getElementsByTagName
将所有 p
元素存储在 HTMLCollection 对象中,然后使用简单的循环更改每个元素实例的样式。
示例:
var pTag = document.getElementsByTagName("p");
for(var i=0; i<pTag.length; i++)
{
pTag[i].style.color = "#4040c9";
}
关于javascript - 尝试将所有 "p"标签更改为以下代码中指定的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57793798/