为了账号安全,请及时绑定邮箱和手机立即绑定

js代码下拉框没有出来,大神帮看下代码哪里有问题(新手求教~)

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>bing 搜索框制作</title>

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

<style type="text/css">

* {

padding: 0;

margin: 0;

}

body {

background-color: #333;

/*height: 620px;*/

}

#bg-div {

width: 1228px;

height: 600px;

background: url("river.jpg") no-repeat;

margin: 0 auto;

/*position: relative;*/

}

#search {

position: absolute;

left: 315px;

top: 200px;

}


.logo {

width: 107px;

height: 53px;

background: url("logo.png") no-repeat;

float: left;

margin: -4px 18px 0 0;

}

form {

float: left;

background-color: #fff;

/*margin-left: 15px;*/

padding: 5px;

}

input[type="text"] {

width: 300px;

height: 30px;

line-height: 30px;

outline: none;/*只有chrome点击进入文本有输入框轮廓outline*/

padding-left: 5px;

border: 0;

float: left;/*向左浮动与logo对齐*/

}

input[type="submit"] {

width: 29px;

height: 29px;

background: url("search-button.png") no-repeat center;

/*margin-left: -45px;*/

border: 0;

float: left;/*向左浮动与text对齐*/

cursor: pointer;/*鼠标滑过“搜索”按钮时光标变为“小手”型状*/

}

.suggest {

width: 343px;

background-color: #fff;

border: 1px solid #999;

/*display: none;*/

/*position: absolute;

top: 240px;

left: 440px;*/

}

.suggest ul {

list-style: none;

}

.suggest ul li {

padding: 3px;

font-size: 14px;

line-height: 25px;

cursor: pointer;

}

.suggest ul li:hover {

background-color: #e5e5e5;

text-decoration: underline;

}

</style>

</head>

<body>

<div id="bg-div">

<div id="search">

<div></div>

<form action="" id="search_form">

<input type="text" id="search_input" placeholder="请输入搜索内容" />

<input type="submit" value />

</form>

</div>

</div>

<div id="search_suggest" style="display: none;">

<ul id="search_result">

<li>搜索结果1</li>

<li>搜索结果2</li>

<li>搜索结果3</li>

<li>搜索结果4</li>

</ul>

</div>


<script>

var getDOM = function (id) {

return document.getElementById(id);

}


var addEvent = function (id, event, fn) {

var el = getDOM(id) || document;

if(el.addEventListener) {//非IE浏览器

el.addEventListener(event, fn, false);

} else if(el.attachEvent) {//IE浏览器

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

}

}


var getElementLeft = function (element) {

var actualLeft = element.offsetLeft;//offsetLeft是获得距离父元素左边的距离

var current = element.offsetParent;


while(current !== null) {

actualLeft += current.offsetLeft;

current = current.offsetParent;

}

return actualLeft;

}


var getElementTop = function (element) {

var actualTop = element.offsetTop;

var current = element.offsetParent;


while(current !== null) {

actualTop += current.offsetTop;

current = element.offsetParent;

}

return actualTop;

}


addEvent('search_input', 'keyup', function () {

getDOM('search_suggest').style.top = getElementTop(getDOM('search_form')) + 38 + 'px';

getDOM('search_suggest').style.left = getElementLeft(getDOM('search_form')) + 'px';

getDOM('search_suggest').style.position = 'absolute';

getDOM('search_suggest').style.display = 'block';

});

</script>

</body>

</html>


正在回答

1 回答

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>bing 搜索框制作</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <style type="text/css">
        * {
            padding: 0;
            margin: 0;
        }
        body {
            background-color: #333;
            /*height: 620px;*/
        }
        #bg-div {
            width: 1228px;
            height: 600px;
            background: url("river.jpg") no-repeat;
            margin: 0 auto;
            /*position: relative;*/
        }
        #search {
            position: absolute;
            left: 315px;
            top: 200px;
        }

        .logo {
            width: 107px;
            height: 53px;
            background: url("logo.png") no-repeat;
            float: left;
            margin: -4px 18px 0 0;
        }
        form {
            float: left;
            background-color: #fff;
            /*margin-left: 15px;*/
            padding: 5px;
        }
        input[type="text"] {
            width: 300px;
            height: 30px;
            line-height: 30px;
            outline: none;/*只有chrome点击进入文本有输入框轮廓outline*/
            padding-left: 5px;
            border: 0;
            float: left;/*向左浮动与logo对齐*/
        }
        input[type="submit"] {
            width: 29px;
            height: 29px;
            background: url("search-button.png") no-repeat center;
            /*margin-left: -45px;*/
            border: 0;
            float: left;/*向左浮动与text对齐*/
            cursor: pointer;/*鼠标滑过“搜索”按钮时光标变为“小手”型状*/
        }
        .suggest {
            width: 343px;
            background-color: #fff;
            border: 1px solid #999;
            /*display: none;*/
            /*position: absolute;
            top: 240px;
            left: 440px;*/
        }
        .suggest ul {
            list-style: none;
        }
        .suggest ul li {
            padding: 3px;
            font-size: 14px;
            line-height: 25px;
            cursor: pointer;
        }
        .suggest ul li:hover {
            background-color: #e5e5e5;
            text-decoration: underline;
        }
    </style>
</head>
<body>
<div id="bg-div">
    <div id="search">
        <div></div>
        <form action="" id="search_form">
            <input type="text" id="search_input" placeholder="请输入搜索内容" />
            <input type="submit" value />
        </form>
    </div>
</div>
<div id="search_suggest" class="suggest" style="display: none;">
    <ul id="search_result">
        <li>搜索结果1</li>
        <li>搜索结果2</li>
        <li>搜索结果3</li>
        <li>搜索结果4</li>
    </ul>
</div>

<script>
    var getDOM = function (id) {
        return document.getElementById(id);
    }

    var addEvent = function (id, event, fn) {
        var el = getDOM(id) || document;
        if(el.addEventListener) {//非IE浏览器
            el.addEventListener(event, fn, false);
        } else if(el.attachEvent) {//IE浏览器
            el.attachEvent('on' + event, fn);
        }
    }

    var getElementLeft = function (element) {
        var actualLeft = element.offsetLeft;//offsetLeft是获得距离父元素左边的距离
        var current = element.offsetParent;

        while(current !== null) {
            actualLeft += current.offsetLeft;
            current = current.offsetParent;
        }
        return actualLeft;
    }

    var getElementTop = function (element) {
        var actualTop = element.offsetTop;
        var current = element.offsetParent;

        if(current !== null) {
            actualTop += current.offsetTop;
            current = element.offsetParent;
        }
        return actualTop;
    }
    var search_suggest = getDOM('search_suggest');
    var search_form = getDOM('search_form');
    addEvent('search_input', 'keyup', function () {
        search_suggest.style.top = getElementTop(search_form) + 38 + 'px';
        search_suggest.style.left = getElementLeft(search_form) + 'px';
        search_suggest.style.position = 'absolute';
        search_suggest.style.display = 'block';
    });
</script>
</body>
</html>

你把 if 写成while  浏览器被你搞死好几次

0 回复 有任何疑惑可以回复我~
#1

IHaveADreamD 提问者

汗~~~very very 3Q
2015-12-15 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
搜索框制作
  • 参与学习       66040    人
  • 解答问题       431    个

本课程从简入深讲解搜索框的制作,学习JQ与JS实现Ajax技术的不同点

进入课程

js代码下拉框没有出来,大神帮看下代码哪里有问题(新手求教~)

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信