这篇主要分析imeBindInput函数。
现在才发现,网上的解压代码有些问题,由于我没研究过代码的压缩算法,对于解压也不是很熟悉,所以暂时没精力去精确还原全部代码,既然这篇文章分析的是imeBindInput函数,那么就直接从内存里面把代码请出来吧:
imeBindInputfunction imeBindInput(a) {
try {
var b = a.getElementsByTagName("input");
for (var i = 0; i < b.length; i++) {
if (b[i].type == "text") {
b[i].imebind = true;
b[i].onkeyup = null;
b[i].onkeydown = null;
b[i].onkeypress = null;
b[i].onfocus = null;
b[i].onblur = null;
b[i].onclick = null;
if (document.addEventListener) {
b[i].addEventListener("keyup", imeInput, true);
b[i].addEventListener("keydown", imeKeyDown, true);
b[i].addEventListener("keypress", imeKeyPress, true);
} else if (document.attachEvent) {
b[i].detachEvent("onkeydown", b[i].onkeydown);
b[i].detachEvent("onkeyup", b[i].onkeyup);
b[i].detachEvent("onkeypress", b[i].onkeypress);
b[i].detachEvent("onfocus", b[i].onfocus);
b[i].detachEvent("onblur", b[i].onblur);
b[i].attachEvent("onkeyup", imeInput);
b[i].attachEvent("onkeydown", imeKeyDown);
b[i].attachEvent("onkeypress", imeKeyPress);
} else {
var c = b[i].onkeyup ? b[i].onkeyup : function () {};
b[i].onkeyup = function () {c();imeInput();};
var d = b[i].onkeydown ? b[i].onkeydown : function () {};
b[i].onkeydown = function () {d();imeKeyDown();};
var f = b[i].onkeypress ? b[i].onkeypress : function () {};
b[i].onkeypress = function () {f();imeKeyPress();};
}
}
}
var g = a.getElementsByTagName("textarea");
for (var i = 0; i < g.length; i++) {
g[i].imebind = true;
g[i].onkeyup = null;
g[i].onkeydown = null;
g[i].onkeypress = null;
g[i].onfocus = null;
g[i].onblur = null;
g[i].onclick = null;
if (document.addEventListener) {
g[i].addEventListener("keyup", imeInput, true);
g[i].addEventListener("keydown", imeKeyDown, true);
g[i].addEventListener("keypress", imeKeyPress, true);
} else if (document.attachEvent) {
g[i].detachEvent("onkeydown", g[i].onkeydown);
g[i].detachEvent("onkeyup", g[i].onkeyup);
g[i].detachEvent("onkeypress", g[i].onkeypress);
g[i].detachEvent("onfocus", g[i].onfocus);
g[i].detachEvent("onblur", g[i].onblur);
g[i].attachEvent("onkeyup", imeInput);
g[i].attachEvent("onkeydown", imeKeyDown);
g[i].attachEvent("onkeypress", imeKeyPress);
} else {
var c = g[i].onkeyup ? g[i].onkeyup : function () {};
g[i].onkeyup = function () {c();imeInput();};
var d = g[i].onkeydown ? g[i].onkeydown : function () {};
g[i].onkeydown = function () {d();imeKeyDown();};
var f = g[i].onkeypress ? g[i].onkeypress : function () {};
g[i].onkeypress = function () {f();imeKeyPress();};
}
}
if (a.addEventListener) {
a.addEventListener("keydown", imeBodyKeyDown, true);
a.addEventListener("mousedown", imeBodyMouseDown, true);
} else if (a.attachEvent) {
a.attachEvent("onkeydown", imeBodyKeyDown);
a.attachEvent("onmousedown", imeBodyMouseDown);
a.attachEvent("onactivate", imeBodyActive);
} else {
var l = a.onmousedown ? a.onmousedown : function () {};
a.onmousedown = function () {l();imeBodyMouseDown();};
}
var m = a.getElementsByTagName("iframe");
for (var i = 0; i < m.length; i++) {
try {
if (m[i].contentDocument) {
var a = m[i];
ime_iframe_arr.push(a);
a.imebind = true;
var a = a.contentDocument;
imeBindInput(a);
} else if (m[i].contentWindow.document) {
var a = m[i];
ime_iframe_arr.push(a);
a.imebind = true;
var a = a.contentWindow.document;
imeBindInput(a);
}
} catch (e) {
}
}
} catch (e) {
}
}
先从大局入手: 可以看出,绑定的地方有:1.type=text的输入框2.textarea3.iframe另外对顶层元素(这里是document)进行事件绑定:1.如果是标准浏览器则绑定keydown/mousedown2.如果是ie浏览器则追加activate事件3.其它的则仅仅追加mousedown事件PS:这个函数采用了try语句,看来作者对这一块操作也不是很有信心啊,呵呵~~先来看看最简单的text输入框是怎么绑定事件的吧。