gpt4 book ai didi

jquery - 从函数内部获取索引

转载 作者:行者123 更新时间:2023-12-01 07:50:00 26 4
gpt4 key购买 nike

使用以下代码,我尝试从绑定(bind)到 play[index].click() 的函数内部访问 index

我一直在尝试一些事情,但无法弄清楚。

jQuery(document).ready(function($) {
$( ".player" ).each(function( index ) {
if (typeof play === 'undefined') {
play = [];
}

play[index] = $(this).children('.button.play');

play[index].on('click', function(e, index) {
e.preventDefault();

// index where are you?
console.log( index );

});
});
});

编辑:

Html 会像这样:

<div class="player">
<a class="button play" href="" title=""></a>
</div>

最佳答案

函数(e,索引){中。您正在函数内部分配“index”,所以现在这是另一个变量。不需要这样做,只需删除 (e, index)

    //index here
play[index].on('click', function(e) {
e.preventDefault();
// index where are you?
console.log( index );

});

关于jquery - 从函数内部获取索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31085347/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com