关于下拉列表

来源:3-4 作业题

田马达加斯加

2018-12-04 10:13:02

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<meta http-equiv="X-UA-compatible" content="IE=edge">

<title>bing搜索</title>

<style type="text/css">

body{

background:#333;

}

.bg-image{

background:url(image/river.jpg);

width:1286px;

height:768px;

margin:0 auto;

}

.search-box{

position:absolute;

left:370px;

top:200px;

}

.logo{

background:url(image/logo.png);

width:107px;

height:53px;

float:left;

margin:-7px 5px 0 0;

}

form{

float:left;

background:#fff;

padding:5px;

}

.input-search-text{

border:none;

outline:none;

width:350px;

height:30px;

float:left;

}

.input-search-button{

border:none;

outline:none;

background:url(image/search-button.png);

width:29px;

height:29px;

float:left;

}

.suggest{

/*position:absolute;

left:200px;

top:250px;*/

width:389px;

background:#fff;

border:1px solid #999;

}

.suggest ul{

list-style:none;

margin:0;

padding:0;


}

.suggest ul li{

padding:5px;

}

.suggest ul li:hover{

text-decoration:underline;

cursor:pointer;

background:#e5e5e5;

}

</style>

</head>

<body>

<div class="bg-image">

<div class="search-box">

<div class="logo"></div>

<form action="https://www.bing.com/search" method="get" id="search-form">

<input type="text" class="input-search-text" name="q" autocomplete="off" id="search-text"/>

<input type="submit" class="input-search-button" value=""/>

</form>

</div>

</div>

<div class="suggest" id="search-suggest" style="display:none;">

<ul id="suggest-list">

<li>提示搜索1</li>

<li>提示搜索2</li>

<li>提示搜索3</li>

</ul>

</div>

<script type="text/javascript">

//封装一个获取dom元素的函数。

function getDom(id){

return document.getElementById('id');

}

// 封装一个兼容各浏览器绑定事件的函数

function addEvent(id,event,fn){

var el=getDom(id) || document;

if(el.addEventListener){

el.addEventListener(event,fn,false);

}else{

el.attachEvent('on'+event,fn);

}

}

// 封装一个函数计算元素距浏览器上边和左边的距离。

function getElementLeft(element){

var actualLeft=element.offsetLeft;

var current=element.offsetParent();

while(current!==null){

actualLeft+=actualLeft.offsetLeft;

current=current.offsetParent();

}

return actualLeft;

}

function getElementTop(element){

var actualTop=element.offsetTop;

var current=element.offsetParent();

while(current!==null){

actualTop+=actualTop.offsetTop;

current=current.offsetParent();

}

return actualTop;

}

addEvent('search-text','keyup',function(){

getDom('search-suggest').style.left=getElementLeft(getDom('search-form'))+38+'px';

getDom('search-suggest').style.top=getElementTop(getDom('search-form'))+38+'px';

getDom('search-suggest').style.position='absolute';

getDom('search-suggest').style.display='block';

});

</script>

</body>

</html>

这个代码老报错,麻烦老师看一下哪里有问题

写回答

1回答

好帮手慕星星

2018-12-04

1、获取元素的时候参数使用错误:

http://img.mukewang.com/climg/5c05e91800018ceb04690104.jpg

id已经是一个变量了,直接使用即可,添加引号就是字符串了。正确的应该是:

http://img.mukewang.com/climg/5c05e90d0001c11b07410130.jpg

2、方法使用元素错误:

http://img.mukewang.com/climg/5c05e95f000159b103930050.jpg

这是jquery元素的方法,而element是dom元素,所以使用的时候会报错。

3、建议将下拉框直接放在搜索框里面,这样比较好定位:

http://img.mukewang.com/climg/5c05e9a400018e5813940485.jpg

js代码:

http://img.mukewang.com/climg/5c05e9c30001d8f310370690.jpg

http://img.mukewang.com/climg/5c05e9da0001196e14390209.jpg

自己完善测试下,祝学习愉快!

0

0 学习 · 4826 问题

查看课程