MediaWiki:Minerva.js: Difference between revisions
Chinaenglish (talk | contribs) No edit summary Tags: Reverted Mobile edit Mobile web edit |
Chinaenglish (talk | contribs) No edit summary Tag: Reverted |
||
| Line 84: | Line 84: | ||
// 修复 Minerva 皮肤下 InputBox 首次点击被主搜索框抢焦点的问题 | // 修复 Minerva 皮肤下 InputBox 首次点击被主搜索框抢焦点的问题 | ||
( function () { | ( function () { | ||
// | // 仅在 Minerva 皮肤下执行 | ||
if ( mw.config.get( 'skin' ) !== 'minerva' ) { | |||
return; | |||
} | |||
// | var customInputId = 'homeSearchInput'; | ||
var originalFocus = HTMLInputElement.prototype.focus; | |||
// | |||
if ( | // 重写所有 input 元素的 focus 方法 | ||
HTMLInputElement.prototype.focus = function () { | |||
// 当前正在被聚焦的元素(this)如果不是我们的自定义搜索框 | |||
if ( this.id !== customInputId ) { | |||
var active = document.activeElement; | |||
// 检查当前实际获得焦点的元素是否是我们的自定义搜索框 | |||
if ( active && active.id === customInputId ) { | |||
// 如果是,说明有脚本(Minerva 组件)正试图从我们的输入框抢走焦点 | |||
// 直接终止这次 focus 操作,不执行原始逻辑 | |||
return; | |||
} | |||
} | } | ||
// | // 其他所有情况(例如正常点击主搜索框、页面初始化聚焦等),放行原始方法 | ||
return | return originalFocus.apply( this, arguments ); | ||
}; | }; | ||
} )(); | } )(); | ||