代码ie8下报错,有什么办法吗?
在其它ie版本和Mozilla Firefox都没有问题,但是在ie8种出错,指向dataShow.innerHTML = arr.join('');这一句。
怎么办?
全部代码:
var xmlHttp = false;
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e2) {
xmlHttp = false;
}
}
if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
xmlHttp = new XMLHttpRequest();
}
//以下为企业名称取值
var myname;
var dataShow;
function callServer(id) {
Init(id);
var url = "getcorp.aspx?myname=" + escape(myname.value);
xmlHttp.open("get", url, true); //建立新请求
xmlHttp.onreadystatechange = updatePage; //指定回调方法
xmlHttp.send(null);
}
var keys = new Array();
function updatePage() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
keys = xmlHttp.responseText.split(','); //ajax返回的数据
getSearchKeys();
}
}
}
function Init(id) {
myname = document.getElementById("myname");
dataShow = document.getElementById("dataShow");
var p = getAbsPoint(myname);
dataShow.style.left = p.x + 'px';
dataShow.style.top = p.y + myname.offsetHeight + 'px';
myname.onclick = dataShow.onclick = function (e) {
e = e || event;
var t = e.target || e.srcElement
if (t.tagName.toLowerCase() == 'li') {
myname.type = 'text'; //为元素指定类型
myname.value = t.innerHTML.replace(/<.+?>/gim, '').replace(/^\s+|\s+$/g, ""); //为元素添加值可用innerHTML.replace(/<.+?>/gim,'')去除标签,用replace(/^\s+|\s+$/g,"")去空格
dataShow.style.display = "none";
WritedataText()
return;
}
if (e && e.stopPropagation) {
//W3C取消冒泡事件
e.stopPropagation();
} else {
//IE取消冒泡事件
window.event.cancelBubble = true;
}
};
document.body.onclick = function (e) {
dataShow.style.display = "none";
};
};
function getSearchKeys() {
var s = myname.value;
if (s == '') {
dataShow.style.display = "none";
return;
}
var arr = ['<ul>'];
for (var i = 0; i < keys.length; i++) {
if (keys[i].indexOf(s) >= 0) {
arr.push('<li>' + keys[i] + '</li>');
}
}
if (arr.length > 1) {
arr.push('</ul>');
dataShow.innerHTML = arr.join('');
dataShow.style.display = "block";
} else {
dataShow.style.display = "none";
}
}
function getAbsPoint(e) {
var x = e.offsetLeft;
var y = e.offsetTop;
while (e = e.offsetParent) {
x += e.offsetLeft;
y += e.offsetTop;
}
return { "x": x, "y": y };
}
------解决方案--------------------
呵呵,找到原因了。把div放在form外面就行了。