我在jQuery UI手风琴中看到你可以使用空格键来切换活动标题。如何禁用此功能?我不希望用户使用键盘与手风琴进行交互。
答案 0 :(得分:6)
如果你根本不需要“_keydown”功能,我想你可以删除它。
delete($.ui.accordion.prototype._keydown);
如果要更改或覆盖“_keydown”功能的功能 并且不想将其破解为您可以执行的原始文件:
$.ui.accordion.prototype._keydown = function( event ) {
// your new code for the "_keydown" function
};
希望有所帮助
答案 1 :(得分:3)
我开发了一个只启用空格键的答案,但它可以针对您想要覆盖的其他keydown事件进行扩展。
/*
* Detect spacebar and return immediately, otherwise call standard behaviour
* The 'solution' of deleting the event handler caused other errors
* http://stackoverflow.com/a/7008791
*/
$.ui.accordion.prototype._originalKeyDown = $.ui.accordion.prototype._keydown;
$.ui.accordion.prototype._keydown = function( event ) {
var keyCode = $.ui.keyCode;
if (event.keyCode == keyCode.SPACE) {
return;
}
// call the original method
this._originalKeyDown(event);
};
答案 2 :(得分:1)
我找到了一个有效的解决方案,但我不确定后果。
在jquery.ui.accordion.js:
_keydown: function( event ) {
if ( this.options.disabled || event.altKey || event.ctrlKey ) {
return;
}
var keyCode = $.ui.keyCode,
length = this.headers.length,
currentIndex = this.headers.index( event.target ),
toFocus = false;
switch ( event.keyCode ) {
case keyCode.RIGHT:
case keyCode.DOWN:
toFocus = this.headers[ ( currentIndex + 1 ) % length ];
break;
case keyCode.LEFT:
case keyCode.UP:
toFocus = this.headers[ ( currentIndex - 1 + length ) % length ];
break;
case keyCode.SPACE:
case keyCode.ENTER:
this._clickHandler( { target: event.target }, event.target );
event.preventDefault();
}
if ( toFocus ) {
$( event.target ).attr( "tabIndex", -1 );
$( toFocus ).attr( "tabIndex", 0 );
toFocus.focus();
return false;
}
return true;
},
注意从空间“跌落”进入。我加了一个休息时间:
_keydown: function( event ) {
if ( this.options.disabled || event.altKey || event.ctrlKey ) {
return;
}
var keyCode = $.ui.keyCode,
length = this.headers.length,
currentIndex = this.headers.index( event.target ),
toFocus = false;
switch ( event.keyCode ) {
case keyCode.RIGHT:
case keyCode.DOWN:
toFocus = this.headers[ ( currentIndex + 1 ) % length ];
break;
case keyCode.LEFT:
case keyCode.UP:
toFocus = this.headers[ ( currentIndex - 1 + length ) % length ];
break;
case keyCode.SPACE:
break;
case keyCode.ENTER:
this._clickHandler( { target: event.target }, event.target );
event.preventDefault();
}
if ( toFocus ) {
$( event.target ).attr( "tabIndex", -1 );
$( toFocus ).attr( "tabIndex", 0 );
toFocus.focus();
return false;
}
return true;
},
按“输入”时仍然会收到关闭行为,所以如果有必要,请随意打破。我认为问题在于
this._clickHandler( { target: event.target }, event.target );
但我在第一次阅读时没有看到它。这个编辑对我有用。
希望有所帮助