2015-10-04 49 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. */ 
}