你如何使$(this)选择器关注当前元素? [英] How do you make the $(this) selector focus on current element?
问题描述
你如何使 $(this)
选择器专注于当前元素?在这个jsfiddle中,我已经挂载了它只进入一个指定的元素,所以你不能按下回车键来激活你所在的按钮。 http://jsfiddle.net/mathzarro/gd6Ep/1/
继承人的棘手部分: $(button:first)。trigger('focus');
我已经说过,我是作为一个表情登上的!原来的编码是伊恩,这里是链接..谢谢@伊恩! http://jsfiddle.net/Vtn5Y/
真正的问题是由HazMat提到的,你关注的是错误的元素(总是使用 $(button:first)。焦点);
。
调用 liSelected.trigger('focus');
在你的keydown处理程序的结尾,并删除其他调用 $(button:first)。trigger('focus');
将解决问题。 >
您还有另一个问题
$ b $ $ $ $ $(button:eq(1) ).click(function(){
//为什么要调用这个?删除这行
$(button:eq(0))。trigger('click');
更新($(span:last));
});
< a href =http://jsfiddle.net/gd6Ep/5/ =nofollow noreferrer>下面是一个工作示例
另外, jsfiddle是伟大的,但你应该张贴代码相关的代码h
您发布的代码患有 以下是代码 How do you make the Heres the tricky part: Ps. I've said I mounted as an expression! The original coder was Ian, here it is the link.. thanks @Ian! http://jsfiddle.net/Vtn5Y/ The real problem was mentioned by HazMat, you were focusing on the wrong element (always the first button using Calling You also have another problem Also, the jsfiddle is great but you should post the code relevant code here too. The code you posted suffers from brittle queries, internal coupling, that is, it's not very flexible to changing HTML structures. I've re-worked your code so that it's in better shape. Here are the main features Here's the code
这篇关于你如何使$(this)选择器关注当前元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
var buttons = $('button') ;
var spans = $('span');
//点击按钮时更新范围
buttons.click(function(e){
var span = $(spans [Array.prototype.indexOf.call(buttons ,document.activeElement)]);
span.text(parseInt(span.text(),10)+ 1);
});
//处理导航,把焦点放在下一个元素
$(window).keydown(function(e){
var isLeft = e.which === 38 || e.which === 37,
isRight = e.which === 40 || e.which === 39;
if(isLeft || isRight){
var currentButtonIndex = Array.prototype.indexOf.call(buttons,document.activeElement);
var nextButtonIndex;
if(currentButtonIndex === -1){
nextButtonIndex = 0;
} else {
var toAdd = isLeft?-1:1;
nextButtonIndex =(currentButtonIndex + toAdd + buttons.length)%buttons.length;
}
buttons [nextButtonIndex] .focus();
}
});
$(this)
selector focus on current element? In this jsfiddle, I've mounted it only goes in a specified element, so you cant press enter to activate the button you 'are in'. http://jsfiddle.net/mathzarro/gd6Ep/1/$("button:first").trigger('focus');
$("button:first").trigger('focus');
. liSelected.trigger('focus');
at the end of your keydown handler and removing the other calls to $("button:first").trigger('focus');
will fix the problem.$("button:eq(1)").click(function () {
// Why are you calling this? Remove this line
$("button:eq(0)").trigger('click');
update($("span:last"));
});
var buttons = $('button');
var spans = $('span');
// Update the span when button is clicked
buttons.click(function(e){
var span = $(spans[Array.prototype.indexOf.call(buttons, document.activeElement)]);
span.text(parseInt(span.text(), 10) + 1);
});
// Handle the navigation, set focus on the next element
$(window).keydown(function(e){
var isLeft = e.which === 38 || e.which === 37,
isRight = e.which === 40 || e.which === 39;
if(isLeft || isRight){
var currentButtonIndex = Array.prototype.indexOf.call(buttons, document.activeElement);
var nextButtonIndex;
if (currentButtonIndex === -1) {
nextButtonIndex = 0;
} else {
var toAdd = isLeft ? -1 : 1;
nextButtonIndex = (currentButtonIndex + toAdd + buttons.length) % buttons.length;
}
buttons[nextButtonIndex].focus();
}
});