1

サーバーページに ajax 呼び出しを行い、結果を html メソッドを介して div に出力しています。

$(document).ready(function()
{
     $.ajax
     ({
    async:false, 
    type: 'GET', cache: false,
    url: '..urlhere..',
        success: function(data){printresult(data);}
      });
});

printresult(データ)を使用:

$("thediv").html(data);

これはすべて機能しますが、結果自体にはクラスのスパンが含まれています。これらのスパンは、ホバリングされたときに単純なアラートをスローする必要があります。私も document.ready でそれを実装しました:

$(".spanclass").hover(function () 
{
    alert('j');
}); 

これは機能しません。結果が ajax から来て、DOM がそれをスパンクラスとして認識しないためですか?

4

4 に答える 4

2

イベントを委任する必要がありますon。メソッドを使用できます。

$(document).on({
   mouseenter: function(){
    alert('entered')
   }, 
   mouseleave: function(){
    alert('left')
  }     
}, ".spanclass")
于 2012-09-06T13:16:21.163 に答える
1

delegate動的に追加された要素に対してこのように設定します

$(function(){
   // on document ready setup the delegate for the hover event
   $(document).on('hover', '.spanclass', function(){
       alert('j');
   }); 
});
于 2012-09-06T13:16:13.730 に答える
1

使用する必要がありますdelegate

$('thediv').delegate('hover', '.spanclass', function() { ... });

これはjQueryからの説明です

Attach a handler to one or more events for all elements that match the selector, now or in the future, based on a specific set of root elements.
于 2012-09-06T13:19:28.867 に答える
0
$('.spanclass').live('mouseover', function() {
    alert('j');
});
于 2012-09-06T13:18:05.917 に答える