Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.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 如何在下面的函数中获取父元素id_Jquery - Fatal编程技术网

Jquery 如何在下面的函数中获取父元素id

Jquery 如何在下面的函数中获取父元素id,jquery,Jquery,我需要在同一个函数和控制台中获取父元素的id 我试图获取绑定函数的父元素的id $('#customized-player').mediaelementplayer({ alwaysShowControls: true, features: ['progress', 'duration', 'tracks' ], audioVolume: 'horizontal', audioWidth: 450, timeForm

我需要在同一个函数和控制台中获取父元素的id

我试图获取绑定函数的父元素的id

$('#customized-player').mediaelementplayer({
        alwaysShowControls: true,
        features: ['progress', 'duration', 'tracks' ],
        audioVolume: 'horizontal',
        audioWidth: 450,
        timeFormat: 'mm:ss',
        startVolume: .6,
        success: function (instance) {
            var thisID = $(this).parents(".audio-player").attr('id');
            console.log("Parent ID is:" + thisID);

        }
    });
我想获得这个“#定制播放器”的父元素的id,正如您将在上面的代码中看到的那样。我已经初始化了一个名为“thisID”的变量,请帮助

提前感谢

通过参考更新您的
成功
功能,如下所示

$('#customized-player').mediaelementplayer({
    alwaysShowControls: true,
    features: ['progress', 'duration', 'tracks' ],
    audioVolume: 'horizontal',
    audioWidth: 450,
    timeFormat: 'mm:ss',
    startVolume: .6,
    success: function (mediaElement, originalNode, instance) {
        var thisID = $(originalNode).parents(".audio-player").attr('id');
        console.log("Parent ID is:" + thisID);
    }
});

那么
$(this).closest(.audioplayer”).attr('id')
呢?还有什么是
$(这个)
?你能
console.log($(this))它可以引用当前函数而不是元素。请确保您知道
$(this)
是什么。如果“未定义”,我是否在正确的位置尝试了代码?关于
$(“#自定义播放器”)。最近的('.audio player.)。attr('id)同样,您只能有一个ID为“customized player”的元素-如果有,则无需在success@AndrejsGubars我需要动态地完成这项工作。因此,关于美元(这个),你是对的。我能在函数内的变量中得到这个“#自定义播放器”吗?或者任何其他合适的方法从这里获取id“$”(“#自定义播放器”)。mediaelementplayer({”在变量中?这是可行的,但我猜OP提到了一个事实,即他有多个
id=“customized player”
,这不应该是这种情况。@andrejsgubar如果OP使用id选择器
(“#自定义播放器”)
然后jquery将始终返回第一个元素,因此他必须将其更新为某个
选择器,而不是
id
。感谢大家的宝贵时间和宝贵意见。这解决了我的问题“var thisID=$(originalNode).parents(.audio player”).attr('id');”再次感谢大家宝贵的时间和帮助