1

だから私がやろうとしているのは、Greasemonkey を使用してテーブルの失われた列のデータを変更することです。

私が抱えている問題は、サイトのナビゲーション バー (合計 3 つのリンクがあります) が JavaScript を使用して新しいページを読み込むことです (つまり、URL は変更されません)。既に行った変更は失われ (コードが再度実行されないため)、更新のみが修正されます。waitForKeyElements()最初のページの読み込みで動作しますが、その後は動作しなくなります。テストするために、 とを追加.clickしました。最初のリンクをクリックすると上にスライドします (つまり、正しく機能します) が、その後は機能しなくなります。パートも同じ。全体をループでラップする必要がありますか? または、x 秒ごとにスクリプトを実行するタイマーを設定する必要があります。.a.p.p

私がこれまでに持っているコード

// ==UserScript==
// @name        changetext
// @namespace   changetext
// @include     *
// @version     1
// @grant       none
// @require     http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js
// @require     https://gist.github.com/raw/2625891/waitForKeyElements.js
// ==/UserScript==


$('p').click(function () {
    $(this).slideUp();
});

$('a').click(function () {
    $(this).slideUp();
});

waitForKeyElements ("#row", newdata());

function newdata()
{
    document.getElementById("row").innerHTML = "<span class='text'>Test</span>";
}
4

1 に答える 1

2

waitForKeyElements.js のソースを見ると、次のようになります。

function waitForKeyElements (
    selectorTxt,    /* Required: The jQuery selector string that
                        specifies the desired element(s).
                    */
    actionFunction, /* Required: The code to run when elements are
                        found. It is passed a jNode to the matched
                        element.
                    */
    bWaitOnce,      /* Optional: If false, will continue to scan for
                        new elements even after the first match is
                        found.
                    */
    iframeSelector  /* Optional: If set, identifies the iframe to
                        search.
                    */
) { ... }

の説明ではactionFunction、関数の実行結果ではなく、関数を渡す必要があることを強く示唆しています。つまり、「実行するコード...」です。

// GOOD - pass the function itself to serve as a callback
waitForKeyElements ("#row", newdata);

// BAD - calls the function once and passes the result (if any) of 
// executing the function
waitForKeyElements ("#row", newdata());

次のように書くこともできます。

waitForKeyElements ("#row", function() {
        document.getElementById("row").innerHTML = "<span class='text'>Test</span>";
    });

さらに見てみると、セレクターにも問題があると思います。スクリプトは、指定されたセレクターが追加されたときに一致する新しい要素を起動することになっています。ただし、ID セレクターがあり、ドキュメントごとに 1 つの ID しか存在できません。

代わりにクラス名を使用するこのスクリプトを試してください: http://jsfiddle.net/Lh438/

于 2013-04-29T23:50:35.910 に答える