0

所有していないサイトに JavaScript を挿入しようとしています。挿入しようとしているコードは次のとおりです。

function submitFormForTime(time){
    $(".t_h").each(function(i, obj){ // find each element with the class t_h
        if ($(this).text() != time) return; // not the right one
        $(this).parent().find("form").each(function(i, obj){
            obj.submit(); // submit that form
        });
    });
}

これは動作しません。私が使用している方法はstringByEvaluatingJavaScriptFromString、ネストされたブラケットなどでは機能しないようです。シンプルなコードで問題なく動作します。

問題は、コードがまったく挿入されていないことです

私の完全なコードは次のとおりです。

[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"var script = document.createElement('script');"
                            "script.type = 'text/javascript';"
                            "script.text = \"function submitFormForTime(time){ "
                                "$(\".t_h\").each(function(i, obj){"
                                "if ($(this).text() != time) return;"
                                    "$(this).parent().find(\"form\").each(function(i, obj){"
                                        "obj.submit();"
                                    "});"
                                "});"
                            "}\";"
                            "document.getElementsByTagName('head')[0].appendChild(script);"]];

どんな助けでも大歓迎です。ありがとう

4

1 に答える 1

1

多くのトラブルもなく、いくつかの小さなものを見ることができます。書き直しましたので、よろしければお試しください。

[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@
                        "var script = document.createElement('script');"
                        "script.type = 'text/javascript';"
                        "script.text = function submitFormForTime(time) { $('.t_h').each(function(i, obj) { if ($(this).text() == time) $(this).parent().find('form').each(function(i, obj){ obj.submit(); }); }); } document.getElementsByTagName('head')[0].appendChild(script);"]];

そのように機能することを願っています。

基本的に、二重引用符をいくつか削除し、一部を単一引用符に置き換えて、エスケープする必要がないようにしました。そして、すべてのスクリプト テキストを 1 行で書きました。

AND find('form') は、フォームがクラスの場合は find('.form') 、フォームが ID の場合は find('#form') である必要があります。

于 2012-09-01T11:57:51.243 に答える