Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
动态元素上的jQuery方法_Jquery_This_Addclass_Dynamically Generated - Fatal编程技术网

动态元素上的jQuery方法

动态元素上的jQuery方法,jquery,this,addclass,dynamically-generated,Jquery,This,Addclass,Dynamically Generated,无法在动态加载的元素上使用addClass方法或通过$this保存DOM元素的实例。我已经在这些元素上使用.on方法来单击事件,但我无法操作它们 $(document).on("click",".PlayPause",function(){ if($(this).attr('src') == 'img/Play.png'){ $(this).attr('src','img/Pause.png'); var s

无法在动态加载的元素上使用addClass方法或通过$this保存DOM元素的实例。我已经在这些元素上使用.on方法来单击事件,但我无法操作它们

$(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();
            }
        });
使用此函数后,您需要保存$

$(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();
            }
        });

在$.post函数中使用$this而不是$this

代码在哪里。。?