我平时的软件开发中,信息的搜索是经常碰到的,增加搜索关键字提示是提高用户体验的一种很好的办法。今天就介绍下在ASP.NET如何利用AJAX来实现搜索的信息提示!
//IE浏览器
try {
//IE5.0
httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
//IE5.5 以上版本
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) { }
//火狐, Safari 等浏览器
httpRequest = new XMLHttpRequest();
function createAjaxObj() {
var httpRequest = false;
//判断是否包含XMLHttpRequest对象 PS:将来IE高也有可能继承次对象
if (window.XMLHttpRequest) {
//火狐 , Safari 等浏览器
httpRequest = new XMLHttpRequest();
if (httpRequest.overrideMimeType)
httpRequest.overrideMimeType('text/xml');
}//判断是否支持Active控件对象
else if (window.ActiveXObject) {
//IE浏览器
try {
//IE5.0
httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
//IE5.5以上
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) { }
}
}
//返回创建好的AJAX对象
return httpRequest;
}
//IE浏览器
if (navigator.userAgent.indexOf("MSIE") > 0)
{ }
//火狐浏览器
if (isFirefox = navigator.userAgent.indexOf("Firefox") > 0)
{}
function getOs() {
//判断浏览器类型
if (navigator.userAgent.indexOf("MSIE") > 0) {
//此时假设文本框id为'txtSearch'
//为文本框附加IE所支持的事件
document.getElementById('txtSearch').attachEvent("onpropertychange", search);
OsTyep = "MSIE";
} else if (navigator.userAgent.indexOf("Firefox") > 0) {
//此时假设文本框id为'txtSearch'
//为文本框附加火狐所支持的事件
document.getElementById('txtSearch').addEventListener("input", search, false);
OsTyep = "Firefox";
}
}
3.根据浏览器版本给文本框清除对应事件
function ClearOS() {
if (navigator.userAgent.indexOf("MSIE") > 0) {
//此时假设文本框id为'txtSearch'
//为文本框清除IE所支持的事件
document.getElementById('txtSearch').detachEvent("onpropertychange", search);
OsTyep = "MSIE";
} else if (navigator.userAgent.indexOf("Firefox") > 0) {
//此时假设文本框id为'txtSearch'
//为文本框清除火狐所支持的事件
document.getElementById('txtSearch').removeEventListener("input", search, false);
OsTyep = "Firefox";
}
}
<style type="text/css" media="screen">
body
{
font:11px arial;
}
/*设置提示提示列表上的样式表*/
.search_link
{
background-color:#FFFFFF;
cursor: pointer;
line-height:24px;
text-indent:6px;
}
/*设置当鼠标移动到提示列表上的样式表*/
.search_link_over
{
background-color:#E8F2FE;
cursor: pointer;
line-height:24px;
text-indent:6px;
}
/*设置显示搜索提示div的样式表*/
#search_div
{
position:absolute;
background-color:#FFFFFF;
text-align:left;
border:1px solid #000000;
border-top:0px;
display:none;
min-width:553px;
width:553px;
}
/*文本框样式*/
.mainInput {
line-height: 26px;
height: 28px;
width: 550px;
font-size: 16px;
font-family: "微软雅黑", "宋体", Candara;
font-weight: normal;
color: #666;
margin: auto;
border: none;
text-indent: 8px;
}
/*鼠标放上文本框样式*/
.mainInputOver {
width:552px;
height:30px;
border-top-width: 1px;
border-right-width: 1px;
border-bottom-width: 1px;
border-left-width: 1px;
border-top-style: solid;
border-right-style: solid;
border-bottom-style: solid;
border-left-style: solid;
border-top-color: #b7b7b7;
border-right-color: #d0d0d0;
border-bottom-color: #d0d0d0;
border-left-color: #d0d0d0;
}
/*鼠标离开文本框样式*/
.mainInputFocus {
width:552px;
height:30px;
border: 1px solid #41b5f2;
}
/*点击文本框样式*/
.myBorder
{
width:552px;
height:30px;
border-top: 1px solid #CCCCCC;
border-bottom: 1px solid #DDDDDD;
border-left: 1px solid #DDDDDD;
border-right: 1px solid #DDDDDD;
}
</style>