2015-10-04 79 views
4

我'开发搜索引擎我的应用程序,并作为它的一部分,用户可以在这里搜索短语多一些例子 -如何创建可调整大小的DIV搜索引擎

enter image description here

当输入线是这样的: enter image description here

它的高度应该调整,文本光标应该到达下一行。

的问题是,它看起来像在第三行我alogrithm不再工作,它看起来像: enter image description here

我搜索输入的结构就是这样

的Html :

<div id=s"earchDiv"> 
    <input id="searchInput"> 
    </input> 
</div> 

JS:

var sDiv = document.getElementById('searchDiv'); 
    var sInput = document.getElementById('searchInput'); 
    var currH = $(sDiv).height(); 
    $(sDiv).css('height', sDiv.scrollHeight + 'px'); 
    var h = 40 - ($(sDiv).height() % 40); 
    if ($(sDiv).height() % 40 != 0) 
     $(sDiv).css('height', ($(sDiv).height() + h) + 'px'); 

这意味着应该调整div高度。 有人有一些想法或算法,可以在这种情况下工作?

+1

显示更多关于如何创建'autocomplete'和更多'html'的代码 –

+0

看起来你完全不需要javascript,因为它只能用CSS完成。 – dfsq

回答

1

首先,你在你的HTML有一个错误:

<div id=s"earchDiv"> 
    <input id="searchInput"> 
    </input> 
</div> 

应该是这样的:

<div id="searchDiv"> 
    <input id="searchInput"> 
    </input> 
</div> 

后...我不明白你为什么用JS当你可以直接使用CSS风格的searchDiv如下:

.searchDiv { 
    display: block; 
    width: 100%; 
    height: auto; // this is important 
    min-height: 40px; // change this as you wish 
} 
0

你可以通过CSS来做到这一点。

#searchDiv{ 
    overflow:hidden; /* the parents of searchdiv should not have fixed height. */ 
}