2014-06-10 3 views
-4

동적으로로드 된 요소에 addClass 메서드를 사용하거나 $ (this)로 DOM 요소의 인스턴스를 저장할 수 없습니다. 나는 이미 이러한 요소에 대한 click 이벤트에 .on 메서드를 사용하고 있지만이를 조작 할 수는 없습니다.동적 요소의 jQuery 메서드

$(document).on("click",".PlayPause",function(){ 
      if($(this).attr('src') == 'img/Play.png'){ 
       $(this).attr('src','img/Pause.png'); 
       var songId = $(this).parent().siblings('.Top_Container').children('input').val(); 
       $.post('songs.php',{songId : songId}, function(path){ 
        if(globalSong.playState && !($(this).hasClass('prevSelection'))){ 
         $('.prevSelection').attr('src','img/Play.png'); 
         globalSong.pause(); 
         $('.prevSelection').removeClass('prevSelection'); 
        } 
        globalSong = soundManager.createSound({ 
         id: ("sound" + songId), 
         url: (songsPath + path), 
         volume: userPrefVolume 
        }); 
        globalSong.play(); 
        $(this).addClass('prevSelection'); 
       }); 
      } else { 
       $(this).attr('src','img/Play.png'); 
       globalSong.pause(); 
      } 
     }); 
+6

내부

$(document).on("click",".PlayPause",function(){ var $this = $(this); //////Save this here////// if($(this).attr('src') == 'img/Play.png'){ $(this).attr('src','img/Pause.png'); var songId = $(this).parent().siblings('.Top_Container').children('input').val(); $.post('songs.php',{songId : songId}, function(path){ //////////Use $this instead of $(this) inside here/////////// if(globalSong.playState && !($(this).hasClass('prevSelection'))){//$this $('.prevSelection').attr('src','img/Play.png'); globalSong.pause(); $('.prevSelection').removeClass('prevSelection'); } globalSong = soundManager.createSound({ id: ("sound" + songId), url: (songsPath + path), volume: userPrefVolume }); globalSong.play(); $(this).addClass('prevSelection'); //$this }); } else { $(this).attr('src','img/Play.png'); globalSong.pause(); } }); 

사용 $this 대신 $(this) 어디에 코드는 ..? –

답변

0

당신은 함수 뒤에 $(this)을 저장해야합니다()는 $.post() 기능