.on()和AJAX的问题

时间:2014-11-04 21:35:28

标签: javascript jquery ajax

我的标题中有这些行

$( document ).ready(function() {

   $(".thumb").hide();
   $(".thumb").first().show();
   $(".text" ).mouseenter(function() {
   $(this).prev(".thumb").show();
      }).mouseleave(function() {
   $(this).prev(".thumb").hide();
   });

});

它们在页面加载时工作正常,但是一旦我用AJAX加载新的HTML内容,这些行就会被忽略。

在我以前的问题中,一些温柔的人告诉我使用.on() 好的。但是如何将此.on()应用于上面的代码?

我知道这对你们大多数人来说都是一个愚蠢的问题,但我是一个绝对的初学者 如果有人可以提供一些有用的建议,我真的很感激。

1 个答案:

答案 0 :(得分:2)

您的代码将是这样的(可以修改)

  $(document).ready(function() {
     $(".thumb").hide();
     $(".thumb").first().show();
     $(document).on('mouseenter',".text",function(){ $(this).prev(".thumb").show(); })
     $(document).on('mouseleave',".text",function(){ $(this).prev(".thumb").hide(); })
  });