【问题标题】:How to change CSS-classes with Greasemonkey?如何使用 Greasemonkey 更改 CSS 类?
【发布时间】:2012-10-27 16:56:42
【问题描述】:

我一直在尝试编写一个小的 Greasemonkey 脚本,该脚本获取一个类的内容并将其更改为另一个类。
基本上它会改变:

<ul class='user_type_1'>

进入:

<ul class='administrator'>

但是,我对 javascript 和 Greasemonkey 完全不熟悉,我所做的所有研究都让我更加困惑。

理想情况下,我希望有人详细解释我是如何实现这一目标的,而不是仅仅交出一个有效的脚本(尽管目前即使这样也会有所帮助)。

【问题讨论】:

    标签: javascript css class greasemonkey


    【解决方案1】:

    使用 jQuery(一个 javascript 实用程序/库)非常简单。 jQuery 提供了函数addClass()removeClass(),让这一切变得轻而易举。

    这是一个完整的 Firefox Greasemonkey 脚本,它改变了该类:

    // ==UserScript==
    // @name     _Change one class into another.
    // @include  http://YOUR_SERVER.COM/YOUR_PATH/*
    // @require  http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
    // @grant    GM_addStyle   
    // ==/UserScript==
    /*- The @grant directive is needed to work around a design change introduced
        in GM 1.0.   It restores the sandbox.
    */
    //-- Get everything that has the class "user_type_1".
    var userTypeNodes = $(".user_type_1");
    
    userTypeNodes.removeClass ("user_type_1");
    userTypeNodes.addClass ("administrator");
    

    【讨论】:

      猜你喜欢
      • 2013-10-23
      • 2014-10-17
      • 2019-04-25
      • 2015-02-21
      • 2010-10-11
      • 1970-01-01
      • 2015-06-26
      • 2023-02-22
      • 1970-01-01
      相关资源
      最近更新 更多