简体   繁体   English

Google Chrome扩展程序帮助:更改单击标记的背景颜色

[英]Google chrome extension help : change the background-color of the clicked tag

I'm developing this extension for Google chrome and basically what it does is when the extension is activated user can highlight the currently displaying web content of a web page(like when we highlight a PDF). 我正在为Google chrome开发此扩展程序,基本上它是在激活扩展程序时执行的,用户可以突出显示当前显示的网页Web内容(例如突出显示PDF时)。 (There are existing extensions, this is for my learning purpose). (有现有扩展名,这是出于我的学习目的)。

And i'm new to extension development and have less knowledge about the required technical stuff. 而且我是扩展开发的新手,对必需的技术知识了解较少。

As a start i made this simple extension which change the background color of the currently displaying web page when the extension icon is clicked. 首先,我做了一个简单的扩展程序,当单击扩展程序图标时,它会更改当前显示的网页的背景颜色。

code sample of .js file : .js文件的代码示例:

function clickss(e) {
   chrome.tabs.executeScript(null,{code:"document.body.style.backgroundColor='yellow'"});
   window.close();
}

document.addEventListener('DOMContentLoaded', function () {
      clickss();
});

this will change the body background color instantly. 这将立即改变人体背景颜色。 So how can i make/modify this to only change the color of a tag (div , p , h) when user click on the particular tag(when user click on a web page only the tag related to that point of click, should change its background color). 因此,当用户单击特定标签时(当用户单击网页上仅与该点击点相关的标签时,应该更改),如何更改/更改标签的颜色(div,p,h)?其背景颜色)。

As i'm new to this i highly appreciate kind answers. 由于我是新手,因此我非常感谢您的好答案。

Just get the tags and attach a click event listener for each one. 只需获取标签,然后为每个标签附加一个click事件监听器即可。

You can use getElementsByTagName() 您可以使用getElementsByTagName()

var divs = document.getElementsByTagName("div");
for(var d in divs) {
   divs[d].addEventListener('click', clickss);
}

function clickss(e) {
   this.style.backgroudColor = "yellow";
}

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

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