1

古いバージョンの Firefox を使用しています (さまざまな理由で更新できません)。また、jQuery イベント ハンドラーを使用できません (古いブラウザーではメモリ リークが発生するようです)。

したがって、次のコードを使用する必要があります。

<div class="somediv" onclick="dostuff()"></div>

問題は、コードが機能しないことです。なんで?

私のJavascriptコード:

$(document).ready(function() 
{
   function dostuff()
   {
       $('#someotherdiv').html('hello');
   }
});
4

4 に答える 4

4

関数をreadyイベント ハンドラー内に配置したため、そのスコープに対してローカルです。関数をルート レベルに配置して、グローバルに使用できるようにします。

function dostuff() {
  $('#someotherdiv').html('hello');
}

$(document).ready(function() {
  // whatever you need to do when the page has loaded
});
于 2013-04-16T10:15:49.653 に答える
0

以下のように、関数を外側$(document).ready(に配置するだけで、関数を配置する必要はありません$(document).ready(

function dostuff()
   {
       $('#someotherdiv').html('hello');
   }
于 2013-04-16T10:14:22.610 に答える
0

別の方法: jQuerys イベント システムを使用してクリック ハンドラーを割り当てる

$(document).ready(function() {
    $('.somediv').click(function () {
        $('#someotherdiv').html('hello');
    }
});
于 2013-04-16T10:20:22.660 に答える