当焦点不在输入框中时,尝试使下拉框隐藏。我无法弄清楚

时间:2019-12-16 23:53:35

标签: javascript

我正在使用下面的代码,试图弄清楚如何在单击鼠标之外将其隐藏时遇到了很多麻烦。我已经尝试了所有我能想到的方法,但是没有用。

var suggestedkeywords = { id : function(str){return document.getElementById(str)} };
suggestedkeywords.id('searchInput').focus();
var tag = '';
suggestedkeywords.id('searchInput').onkeyup = function(e){
	if(!e.keyCode.toString().match(/^(37|38|39|40|13|16|17|18|224)$/)){
        if(tag!==''){ document.body.removeChild(tag); }
		tag = document.createElement('script');
		var term = suggestedkeywords.id('searchInput').value;
		tag.src = 'https://suggestqueries.google.com/complete/search?client=chrome&callback=suggestedkeywordsComplete&q='+term;
		document.body.appendChild(tag);}};
var suggestedkeywordsComplete = function(data){
	suggestedkeywords.id('autocomplete').innerHTML = '';
	for(var i=0; i<10; i++){
	if(data[1][i]){
suggestedkeywords.id('autocomplete').innerHTML += '<p><a href="https://www.ask.com/web?o=0&l=dir&qo=serpSearchTopBox&q='+data[1][i]+'" class="autocomplete" target="_self">'+data[1][i]+'</a>'+data[2][i]+'</p>';
}}};
<html><head>
<body>



<DIV>
<form action="/" name="query">
<div><span>
<input id="searchInput" name="q" value="" maxlength="1000" aria-label="Enter your search here - Search suggestions will show as you type" type="search" class="search" autofocus title="Enter Search" autocomplete="off" required>
</span>

<span><button>Search</button></span>

<div id="autocomplete" class="autocompletediv"></div>
</form></div>

</body>
</html>

0 个答案:

没有答案