在查找函数中使用 $(this) jquery
Using $(this) inside of a find function jquery
我需要用 class 'controller_hover' 找到一个 div,然后可以用 $(this) 访问它,这样我就可以切换下一个或上一个 div 在控制器左右方向键上。
我有这个右方向键箭头的片段,但它不起作用:
if (event.data.action == "right"){
$(".controller_hover").find(function(){
$(this).next('.select').toggleClass("controller_hover");
$(this).toggleClass("controller_hover");
})
}
HTML
<div class="dialog-alert dialog-visible">
<div class="dialog-border"></div>
<div class="dialog-title">Select player</div>
<div class="dialog-message"></div>
<div class="select controller_hover" data-player="1">Player 1</div>
<div class="select" data-player="2">Player 2</div>
<div class="dialog-close">×</div>
<div class="dialog-clearFloat"></div>
</div>
请将您的全部代码添加到代码片段工具(<> 图标)中,您可以为您的逻辑添加警报。
$(document).ready(function () {
//if (event.data.action == "right"){
$(".controller_hover").find(function(){
alert('hi');
$(this).next('.select').toggleClass("controller_hover");
$(this).toggleClass("controller_hover");
})
//}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="dialog-alert dialog-visible">
<div class="dialog-border"></div>
<div class="dialog-title">Select player</div>
<div class="dialog-message"></div>
<div class="select controller_hover" data-player="1">Player 1</div>
<div class="select" data-player="2">Player 2</div>
<div class="dialog-close">×</div>
<div class="dialog-clearFloat"></div>
</div>
请检查这个link:Handle arrow keys from D-pad on WebView Google Tv app
得到我需要的东西:
if (event.data.action == "right"){
current_player = $(".controller_hover");
next_player = $(".controller_hover").next(".select");
if(next_player.hasClass('select')){
current_player.toggleClass("controller_hover");
next_player.toggleClass("controller_hover");
}
}
我需要用 class 'controller_hover' 找到一个 div,然后可以用 $(this) 访问它,这样我就可以切换下一个或上一个 div 在控制器左右方向键上。
我有这个右方向键箭头的片段,但它不起作用:
if (event.data.action == "right"){
$(".controller_hover").find(function(){
$(this).next('.select').toggleClass("controller_hover");
$(this).toggleClass("controller_hover");
})
}
HTML
<div class="dialog-alert dialog-visible">
<div class="dialog-border"></div>
<div class="dialog-title">Select player</div>
<div class="dialog-message"></div>
<div class="select controller_hover" data-player="1">Player 1</div>
<div class="select" data-player="2">Player 2</div>
<div class="dialog-close">×</div>
<div class="dialog-clearFloat"></div>
</div>
请将您的全部代码添加到代码片段工具(<> 图标)中,您可以为您的逻辑添加警报。
$(document).ready(function () {
//if (event.data.action == "right"){
$(".controller_hover").find(function(){
alert('hi');
$(this).next('.select').toggleClass("controller_hover");
$(this).toggleClass("controller_hover");
})
//}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="dialog-alert dialog-visible">
<div class="dialog-border"></div>
<div class="dialog-title">Select player</div>
<div class="dialog-message"></div>
<div class="select controller_hover" data-player="1">Player 1</div>
<div class="select" data-player="2">Player 2</div>
<div class="dialog-close">×</div>
<div class="dialog-clearFloat"></div>
</div>
请检查这个link:Handle arrow keys from D-pad on WebView Google Tv app
得到我需要的东西:
if (event.data.action == "right"){
current_player = $(".controller_hover");
next_player = $(".controller_hover").next(".select");
if(next_player.hasClass('select')){
current_player.toggleClass("controller_hover");
next_player.toggleClass("controller_hover");
}
}