-1

簡単に言えば、私はグリースモンキースクリプトを作成しています。私が作業しているページには、ページに応じてさまざまな値を持つこの特定のテーブルがあります。

<table class="billing dead">
    <tbody>
        <tr>
            <th>Index:</th>
            <td>Long alpha numeric value here</td>
        </tr>
        <tr>
            <th>Status:</th>
            <td class="status">This text is red to denote urgency.</td>
        </tr>
    </tbody>
</table><!-- end billing dead -->

私がする必要があるのは、テキスト「Index:」を、状況に応じてボタンかリンクかに関係なく、別のテキストに変更することです。Javascriptで実際にアイテムを見つけるのに苦労しています。

周りを見回すと、これが見つかりました:JavaScriptでクラスごとに要素を取得する方法は?これは私にこのコードを提供しました:

function replaceContentInContainer(matchClass,content)
{
    var elems = document.getElementsByTagName('*'), i;
    for (i in elems)
    {
        if((" " + elems[i].className + " ").indexOf(" " + matchClass + " ") > -1)
        {
            elems[i].innerHTML = content;
        }
    }
}

これはテーブル全体を置き換えますが、私の状況に最も近い質問/回答です。それは完全に正しくありません。上にリンクされているそのメソッドのテーブルを見つけたら、その特定のテーブルの要素にアクセスする方法はありますか?恣意的に変えてみました

elems[i].innerHTML = content;

の中へ

elems[i][0].innerHTML = content;

elems[i]それはノードのノードではないことをすぐに教えてくれました。私が探していることを行う簡単な方法はありますか?

4

3 に答える 3

1
document.querySelector('.billing.dead > tbody > tr > th')
        .firstChild
        .data = "new value";
于 2012-07-16T03:45:58.363 に答える
0

それはうまくいきます。これはデモです。

replaceContentInContainer('status', 'hello');

于 2012-07-16T03:36:03.883 に答える
0

複数の「請求」テーブルがありますか?「インデックス」は1つだけで、常に最初の行ですか?

次の完全なスクリプトは、結果のボタンのアクティブ化を含め、すべての「インデックス」行に関するHTMLを変更する方法を示しています。jQueryを使用しているため、時間と労力を大幅に節約できます。

jsFiddleで実際のベースコードを参照してください。

// ==UserScript==
// @name     _Buttonize the Index cells
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// ==/UserScript==

//--- Find all the "Index" headers.
var indexHdrs   = $("table.billing.dead th:contains(Index)");

//--- Wrap them all in a button, for example.
indexHdrs.each ( function () {
    $(this).wrapInner ('<button class="myButton" />');
} );

/*--- Activate the buttons.  In this case they just display matching
    cell contents.
*/
$("table.billing.dead th button.myButton").click ( function () {
    var jThis           = $(this);  //-- The button that was clicked.
    var matchingCell    = jThis.parent ().next ();
    alert (matchingCell.text () );
} );


注:Chromeでこのスクリプトを使用するには、Tampermonkey拡張機能をインストールします。これは、追加機能と、ユーザースクリプトのインストールに関するChromeの保留中の制限の両方で実行する必要があります。

于 2012-07-16T04:34:17.727 に答える