简体   繁体   English

如何更改字体颜色-Chrome扩展程序?

[英]how to change Font Color- Chrome Extension?

How can i change the font color of a text inside a tag using a chrome extension: 如何使用chrome扩展名更改标签内文本的字体颜色:

this is how my manifest looks like: 这是我的清单的样子:

{
 "name":"Change Font Color",
 "description":"Changes font color to red",
 "version":"1",
 "manifest_version":2,
 "content_scripts": [
    {
     "matches": ["http://www.google.com/*","https://www.google.com/*"],
     "js": ["myscript.js"]
    }
   ]
}

JavaScript file: JavaScript文件:

    window.addEventListener("load", function() {
    var x = document.getElementsByClassName('gb_id gb_ac gb_g');
    x.style.color = 'red';
    });

I am trying to change the font color of a sections on google's mani webpage to red but my code does not work. 我正在尝试将Google的Mani网页上某个部分的字体颜色更改为红色,但是我的代码不起作用。

Any thoughts? 有什么想法吗?

UPDATE : 更新

I found the solution. 我找到了解决方案。 There were two issues: 有两个问题:
1) I was trying to grab multiple classes with the below line. 1)我试图用下面的代码抓取多个类。 I should have specified just only one class 我应该只指定一个班级

    `var x = document.getElementsByClassName('gb_id gb_ac gb_g');`

2) I needed to iterate through the collection on classes! 2)我需要遍历类的集合! It returns an array: 它返回一个数组:

for (var i =0,len = r.length; i<len;i++)
{
    x[i].style['color']= 'red';
}

I hope this info comes in handy to someone int he future! 希望此信息对将来的人有用!

Instead of document.getElementsByClassName, use document.querySelectorAll 代替document.getElementsByClassName,使用document.querySelectorAll

 var els = document.querySelectorAll('.gb_id.gb_ac.gb_g');
 for(var i = 0; i < els.length; i++) {
   els[i].style.color="red";
 }

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

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