3

私の悪い英語の言葉で申し訳ありません。

こんにちは、JQuery でカスタム イベントを発生させるときに問題が発生しました。私の問題は:

  • content_scriptsAPIを使用してGoogle chrome拡張機能を開発しようとしています。
  • textAreaでEnterキーを押すと、対象のサイトがコメントを公開しています。
  • クリックすると同じイベントを発生させるボタンを開発する必要があります。

http://jsfiddle.net/エディターを次のように使用して、同じことをシミュレートしようとしました。

         $(document).ready(function(){
// this what the site do when press enter key on textarea , the submit form with ajax to publish a comment.
          $("textarea").bind("keydown",function(e){
            if(e.which == 13){
              alert("enter pressed");
            }
          });
    // this what i need to do, firing the same event when i click on my button which i added by my extension.
          $("button").click(function(){
            var e = $.Event("keydown");
            e.which = 13;
            $("textarea").trigger(e);
          });
        });

上記のコードは問題なく実行されますが、このシミュレーションをターゲット Web サイトに適用すると、何も起こりません。サイトが keydown、keyup、または keypress イベントを使用してコメントを公開するかどうかを想像するので、すべてのイベント タイプを使用しようとしましたが、すべて失敗しました。実際にEnterキーを押してコメントを送信するなど、元のイベントと同じものを起動するにはどうすればよいですか?

4

2 に答える 2

1

コメントを送信する新しい関数を作成し、次の場合に呼び出すことができます。

  • ユーザーがテキストエリアで ENTER を押す
  • ユーザーがボタンをクリック

     $(document).ready(function(){
    
         function submitComment()
         {
             //Do what you want to submit the textarea text
             var comment = $("textarea").html();
         }
    
         $("textarea").bind("keydown",function(e){
            if(e.which == 13){
               submitComment();
            }
         });
    
         $("button").click(function(){
            submitComment();
         });
     });
    
于 2013-01-08T15:50:49.847 に答える
0

ロジックを分離してみませんか?これはテスト済みのコードではないので、いじる必要があるかもしれませんが、この一般的なスタイルは機能し、あなたがしようとしていることを達成します。

function myFunction()
{

    alert("enter pressed");
}

 $(document).ready(function(){
// this what the site do when press enter key on textarea , the submit form with ajax to publish a comment.
          $("textarea").bind("keydown",function(e){
             if(eventArg.which == 13){
                    myFunction();

              }
          });


    // this what i need to do, firing the same event when i click on my button which i added by my extension.
 $("button").click(function(){
         myFunction();
   });
 });
于 2013-01-08T15:55:22.960 に答える