google上搜索时有个关键提示,据但它弹出的图层不会占网页,应该是浮动上面的吧,,怎么实现的呀?
解决方案 »
- SQLServerResultSet.TYPE_SS_SCROLL_DYNAMIC这条语句总是不能通过
- 新手求教html基础问题~!感谢帮助~!
- SSH框架中为什么在dao中添加自己的方法会出错(着急)
- 问个自动生成编号的问题
- 谈谈J2EE程序员换工作的心得
- 用户授权管理方案的问题?
- 在JSP中调用QQ临时对话框时如何判断对方是否在线!
- 关于DataSource的问题?在线等待!!!!!!
- Jsp 中最多可以写多行代码。我写了一个1000多行的代码,打开当前页时把代码调出来了这是怎么回事
- 在jsp文件中, this代表的意思???
- 大家帮我找找这个网站的缺点 http://www.odl-fx.cn/
- 请问webservice如何实现文件上传,下载?
<html>
<head>
<title>输入提示示例</title>
<style>
TD {FONT-SIZE: 12px}
</style>
<script language="javascript">
//创建XMLHttpReques对象
function createXMLHttpRequest() {
if (window.XMLHttpRequest) {
//Mozilla 浏览器
XMLHttpReq = new XMLHttpRequest();
} else{
// IE浏览器
if (window.ActiveXObject) {
try {
XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
}catch (e) {
try {
XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}catch (e) { }
}
}
}
}
//处理服务器响应结果
function handleResponse() {
// 判断对象状态
if (XMLHttpReq.readyState == 4) {
// 信息已经成功返回,开始处理信息
if (XMLHttpReq.status == 200) {
clearTable();
var out = "";
var res = XMLHttpReq.responseXML;
var items = res.getElementsByTagName("item");
for(var i=0;i<items.length;i++)
{
addRow(items(i).firstChild.nodeValue);
}
setDivStyle();
}
}
}
//清除表格中的结果
function clearTable()
{
var content = document.getElementById("content");
while(content.childNodes.length>0)
{
content.removeChild(content.childNodes[0]);
}
}
//向输入提示的表格中添加一行记录
function addRow(item)
{
var content = document.getElementById("content");
var row = document.createElement("tr");
var cell = document.createElement("td");
cell.appendChild(document.createTextNode(item));
cell.onmouseover = function(){this.style.background="blue"};
cell.onmouseout = function(){this.style.background="#f5f5f1"};
cell.onclick = function(){
document.getElementById("key").value=this.innerHTML;
document.getElementById("suggest").style.visibility="hidden"};
row.appendChild(cell);
content.appendChild(row);
}
//发送客户端的请求
function sendRequest(url) {
createXMLHttpRequest();
XMLHttpReq.open("GET", url, true);
//指定响应函数
XMLHttpReq.onreadystatechange = handleResponse;
// 发送请求
XMLHttpReq.send(null);
}
//调用Ajax自动提示功能
function suggest()
{
var key = document.getElementById("key").value;
sendRequest("Suggest?key="+key);
}
//设置输入提示框的位置和风格
function setDivStyle()
{
var suggest = document.getElementById("suggest");
suggest.style.border = "black 1px solid";
suggest.style.left = 62;
suggest.style.top = 50;
suggest.style.width = 150;
suggest.style.backgroundColor = "#f5f5f1"
document.getElementById("suggest").style.visibility="visible"
}
</script>
</head>
<body>
<font size="1">
输入提示示例(可以输入字母a开头的字符串进行测试)<br>
请输入:<input type="text" id="key" name="key" onkeyup="suggest()"/>
<div id="suggest" style="position:absolute">
<table>
<tbody id="content"></tbody>
</table>
</div>
</font>
</body>
</html>
import java.io.PrintWriter;
import java.util.ArrayList;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class Suggest extends HttpServlet {
private ArrayList lib = new ArrayList();
//初始化数据集合,可以在这个字库中添加更多词条
public void init() throws ServletException
{
lib.add("a");
lib.add("able");
lib.add("access");
lib.add("advance");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 设置生成文件的类型和编码格式
response.setContentType("text/xml; charset=UTF-8");
response.setHeader("Cache-Control", "no-cache");
PrintWriter out = response.getWriter();
String output = "";
// 处理接收到的参数
String key = request.getParameter("key");
ArrayList matchList = getMatchString(key);
if(!matchList.isEmpty())
{
output +="<response>";
for(int i=0;i<matchList.size();i++)
{
String match = matchList.get(i).toString();
output +="<item>"+match+"</item>";
}
output +="</response>";
}
out.println(output);
out.close();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
//取得所有匹配的字符串
public ArrayList getMatchString(String key)
{
ArrayList result = new ArrayList();
if(!lib.isEmpty())
{
for(int i=0;i<lib.size();i++)
{
String str = lib.get(i).toString();
if(str.startsWith(key))
result.add(str);
}
}
return result;
}
}