0

Cannot call method 'changeBackgroundColor'未定義のエラーが発生し続けます。プラグイン内でパブリック メソッドを呼び出すようにクリック イベントを設定しました。そのメソッドがプログラムで呼び出されると機能しますが、要素をクリックするとエラーが発生します。

私のプラグインのhtml構造は次のようになります:

<div class="container">
    <div id="square"></div>
</div>

プラグインから抜粋:

this.changeBackgroundColor = function() {
  $('#square').css('background','red'); 
};

//Note "this" is the .container so I bind click event to child square
    $('#square', this).bind('click', function () {
        //Error
       changeBackgroundColor(); 
    });

私が呼び出す$('.container').myPlugin().changeBackgroundColor();と、動作します。しかしchangeBackgroundColor、クリックイベント関数から呼び出すと、関数が見つかりませんchangeBackground

jsFiddle へのリンクは こちら

注:jQuery 1.10.1を使用しています

4

2 に答える 2