【发布时间】:2011-11-22 00:46:52
【问题描述】:
我正在开发一个bookmarklet,它可以让用户用我们的语言在任何输入字段上书写。我们选择Ctrl+M 在默认和我们的语言之间切换布局(受维基百科启发)。它在几乎每个使用 chrome 的网站上都运行良好。当我们开始检查 Firefox 时,我们发现它只在 Facebook 中失败。
此外,Facebook 从
window之外捕获Ctrl+M范围。比如,形成地址栏、搜索栏、萤火虫控制台等。
我尝试过使用原始 javascript、jQuery 以及 John Resig 的 jQuery Hotkeys 插件,但没有运气:(
这是我尝试过的一个版本。您可以在 Firebug 控制台上运行它以进行测试 -
(function(){
var noConflictMode = false;
if(typeof $ !== 'undefined') noConflictMode = true;
if(typeof jQuery === 'undefined') {
var root = (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]);
var ns = document.createElementNS && document.documentElement.namespaceURI;
var script = ns ? document.createElementNS(ns, 'script') : document.createElement('script');
script.type = 'text/javascript';
script.onreadystatechange = function () {
if (this.readyState == 'complete') test();
}
script.onload= test;
script.src= 'https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.min.js';
root.appendChild(script);
} else {
test();
}
function test() {
if(noConflictMode) jQuery.noConflict();
jQuery(window).on('keydown keyup keypress', function(e){
e.preventDefault();
// For Firefox
e.stopPropagation();
// Extra effort :|
e.stopImmediatePropagation()
e.cancelBubble = true;
console.log(e);
return false;
});
}
})();
【问题讨论】:
-
与
Ctrl+M无关,但是如果相关页面已经加载了jQuery 并且不需要 想要noConflict模式怎么办?你的test函数会搞砸页面。 -
@T.J.Crowder Nice Point,我已在我的原始代码 (github.com/torifat/jsAvroPhonetic/blob/master/dist/…) 中修复了它。为了简单起见,我在这里删除了它。
-
.delegate()的第一个参数是一个选择器。你的意思是.bind()? -
@gilly3 是的,抱歉,我在原始代码中的
live中写了bind。转换时我忘记了delegate参数。谢谢,我再次修复:) -
Ctrl+M 在 facebook 中的作用是什么?它似乎对我不起作用
标签: javascript jquery facebook firefox jquery-plugins