简体   繁体   中英

Cannot change style of HTML elements using jQuery by content script

I'm writing an extension for chrome as "Content script". My extension should change the background color of google home page (https://www.google.com) I wrote this code (including jquery):

$(".gsib_a").style="background:#FF0000";

But not worked. I'm sure I added jQuery to content script, and the manifest.json file is set. I am sure because this code works:

$(".gsib_a").hide();

And I am sure changing style of the element with class of gsib_a is exactly what I need and affects. Because I've tested it by Chrome Developer Tools .

Okay, who knows the problem?

if you want to change an attribute, use the attr method : ​$('.gsib_a').attr('style','background-color:#FF0000')​;

but you can change the css directly with the css method :

​$('.gsib_a').css('background-color','#FF0000')​;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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