简体   繁体   English

如何使用Firebug查找CSS属性并使用Javascript更改它

[英]How to use Firebug to find CSS attribute and change it using Javascript

I am currently trying to change the color of the top search box background color on the SO page permanently on my web browser by writing a js script to be used with Greasemonkey. 我目前正在尝试通过编写与Greasemonkey一起使用的js脚本,永久地在我的Web浏览器上更改SO页面上顶部搜索框背景颜色的颜色。 I don't have much experience with Javascript at all, but have not been able to figure out how to properly use Firebug to find out how to reference the particular attribute, which would then allow me to modify it. 我根本没有很多Javascript经验,但是无法弄清楚如何正确使用Firebug来找出如何引用特定属性,然后允许我修改它。 I have tried the following: 我尝试过以下方法:

document.getElementById('search').textbox.style.color = '#FFFFAA';
document.getElementById('search.textbox').style.color = '#FFFFAA';
.
.

and the list of combinations go on, but figure that I am missing out on something fundamental with my Javascript referencing. 并且组合列表继续,但是我想通过我的Javascript引用错过了一些基本的东西。 My Greasemonkey definitely works as the alert() function works on the SO page. 我的Greasemonkey绝对有效,因为alert()函数可以在SO页面上运行。

Here is a snippet of the HTML code: 以下是HTML代码的片段:

<div id="topbar">
    <div id="hlinks">
    <div id="hsearch">
        <form id="search" autocomplete="off" method="get" action="/search">
            <div>
                <input class="textbox" type="text" value="" size="28" maxlength="140" tabindex="1"
                placeholder="search" name="q" autocomplete="off">
            </div>
        </form>
    </div>
</div>

Step by step: 一步步:

  1. Get the search form with: var searchForm = document.getElementById('search'); 获取搜索表单: var searchForm = document.getElementById('search');

  2. Get the input textbox, by using it's name (q): var searchBox = searchForm.q; 获取输入文本框,使用它的名称(q): var searchBox = searchForm.q;

  3. Set the color: searchBox.style.color = '#FFFFAA'; 设置颜色: searchBox.style.color = '#FFFFAA';

Or you can do all in one step: 或者你可以一步到位:
document.getElementById('search').q.style.color = '#FFFFAA';

But please note that if you want to change background color you need to use: 但请注意,如果您想更改背景颜色,则需要使用:
document.getElementById('search').q.style.backgroundColor= '#FFFFAA';

As color changes text color not background. 由于颜色改变文本颜色而不是背景。

Your script isn't working since you're looking for an ID on this page that does not exist. 您的脚本无法正常工作,因为您在此页面上查找的ID不存在。

Examining the markup for the SO page, it looks as follows: 检查SO页面的标记,它看起来如下:

<input autocomplete="off" name="q" class="textbox" placeholder="search" tabindex="1" type="text" maxlength="140" size="28" value="" style="width: 200px; max-width: 200px; ">

Hence we can use the following: 因此我们可以使用以下内容:

document.getElementsByName('q')[0].style.backgroundColor = 'blue';

使用document.getElementById('search').style.color = '#FFFFAA';

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

相关问题 使用JavaScript更改CSS文件属性值 - change css file attribute values using JavaScript 使用 javascript 更改 css class 属性值 - Change css class attribute values using javascript 如何更改 Javascript 中关键帧的 CSS 属性? - How to change a CSS attribute of keyframes in Javascript? 如何在iPad的safari上调试javascript和CSS(我可以在iPad上使用firebug之类的东西)吗? - How can I debug javascript and css on safari for iPad (Can I use something like firebug on iPad)? 如何在浏览器中编辑 javascript,就像我可以使用 Firebug 编辑 CSS/HTML 一样? - How can I edit javascript in my browser like I can use Firebug to edit CSS/HTML? 使用Firebug时如何防止页面更改 - How to Prevent Page Change when using Firebug 如何使用Firebug或任何其他方式“立即”查找/分析正在执行的JavaScript? - How do I find/profile what JavaScript is being executed “right now” using Firebug or any other way? 如何找到具有属性的元素并更改javascript中的属性值? - How to find the element with an attribute and change the attribute value in javascript? 如何避免使用CSS属性选择器的JavaScript - How to avoid using JavaScript with CSS attribute selector 更改CSS下的属性的Javascript - Javascript to change attribute under CSS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM