gpt4 book ai didi

JQuery 1.7.2 焦点事件始终在同一选择器上的模糊事件之前弹出

转载 作者:行者123 更新时间:2023-12-01 04:55:48 24 4
gpt4 key购买 nike

我编写了一些代码来限制我选择的某些元素的选项卡导航。您可以在此处查看完整的代码:http://jsfiddle.net/M2ELL/2/工作正常。

问题是我想“装饰”焦点所在的父 div。为此,我制作了这些事件监听器:

$('[data-mymodaltabindex]').blur(function () {        
$(this).parent().removeClass('highlight');
console.log('blur ' + $(this));
});

$('[data-mymodaltabindex]').focus(function () {
$(this).parent().addClass('highlight');
console.log('highlight ' + $(this));
});

当每个父 div 只有一个输入时,这些将按预期工作。另一方面,当每个父 div 有两个输入(如我的示例中的两个 radio )时,焦点事件始终发生在模糊事件之前。结果是突出显示然后删除 div 上的突出显示,这与我想要的相反。

我希望模糊事件发生在导致删除突出显示然后突出显示之前。我怎样才能做到这一点?为什么在这种情况下焦点事件总是发生在模糊事件之前?

完整代码:

<div>
radio 1
<input type="radio" data-mymodaltabindex="1" />
radio 2
<input type="radio" data-mymodaltabindex="2" />
</div>
<div>
text
<input type="text" data-mymodaltabindex="3" />
</div>

$('[data-mymodaltabindex]').on('keydown', function (e) {
if (e.keyCode == 9 || e.which == 9) {
// define variables
var mytabindex = null;
var maximum = null;
var minimum = null;
var next = null;
var previous = null;
var values = new Array();
// set mytabindex on the actual focused control
var mytabindex = $(this).attr('data-mymodaltabindex');
// put every visible mytabindex into array
$('[data-mymodaltabindex]:visible').each(function () {
values.push(parseInt($(this).attr('data-mymodaltabindex')));
});
//console.log(values);
// look for maximum minimum mytabindex
// for maximum and minimum we filter out null values
// as they are interpreted as 0 with math functions
maximum = Math.max.apply(null, values.filter(function (val) { return val !== null }));
minimum = Math.min.apply(null, values.filter(function (val) { return val !== null }));
// set next and previous using function
next = getModalClosestHighValue(values, mytabindex);
previous = getModalClosestLowValue(values, mytabindex);
// go back to begining / end if
// end / begining is reached
if (!previous) { previous = maximum; }
if (!next) { next = minimum; }
// check if there is shift combination
if (e.shiftKey) {
mytabindex = previous; // focus on the previous item
} else {
mytabindex = next; // focus on the next item
}
// focus on this element
$('[data-mymodaltabindex=' + mytabindex + ']').focus();
console.log('focus set to [data-mymodaltabindex=' + mytabindex + ']');
// stop propagation
return false;
}
});

function getModalClosestHighValue(a, x) {
var hi;
for (var i = a.length; i--; ) {
if (a[i] > x && (hi === undefined || hi > a[i])) hi = a[i];
};
return hi;
}

function getModalClosestLowValue(a, x) {
var lo;
for (var i = a.length; i--; ) {
if (a[i] < x && (lo === undefined || lo < a[i])) lo = a[i];
};
return lo;
}

最佳答案

事实证明,这是某些 jQuery 版本的错误,可以使用 jQuery 1.9 解决。

适用于 1.9:http://jsfiddle.net/7mfLb/

不适用于 1.8.3:http://jsfiddle.net/dHwE5/

<小时/>

如果您想让它与 1.7.2 一起工作,那么我建议您使用以下解决方法:

$('[data-mymodaltabindex]').on('blur focus', function() {
var $p = $(this).parent();
$p.removeClass('highlight');
setTimeout(function() {
$('div :has(:focus)', $p.parent()).addClass('highlight');
}, 0);
});

Demonstration

关于JQuery 1.7.2 焦点事件始终在同一选择器上的模糊事件之前弹出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14477073/

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