2012-08-04 35 views
0

我已经使用javascript实现了一个即时搜索,我能够使它工作,除了在一个点上。JavaScript即时搜索div隐藏与褪色效果

我在我的即时搜索中实现了以下功能,并且工作正常。

  1. 结果出现在“搜索结果”div中。
  2. 当点击文档上的任何地方导致disapper。
  3. 当鼠标悬停在或输入字段结果收割机中单击。
  4. 文件点击后重新出现结果淡化效果。

这1.执行不正常。

文件点击后增加淡化效果消除效果。当文件被点击时第一次工作结果消失效果消失,但鼠标悬停后或单击输入域结果重新出现时,然后在单击文件结果时不会消散和没有效果。

这些是我的Javascript代码。

<script type="text/javascript"> 
    function showResult(str) 
    { 
     if (str.length==0) 
     { 
      document.getElementById("search-result").innerHTML=""; 
      document.getElementById("search-result").style.border="0px"; 
      return; 
     } 
     if (window.XMLHttpRequest) 
     {// code for IE7+, Firefox, Chrome, Opera, Safari 
      xmlhttp=new XMLHttpRequest(); 
     } 
     else 
     {// code for IE6, IE5 
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
     } 
     xmlhttp.onreadystatechange=function() 
     { 
      if (xmlhttp.readyState==4 && xmlhttp.status==200) 
      { 
       document.getElementById("search-result").innerHTML=xmlhttp.responseText; 
       document.getElementById("search-result").style.border="1px solid #A5ACB2"; 
       document.getElementById("search-result").autocomplete="off"; 
       document.getElementById("search-result").style.display="block"; 
       var fired = false; 

       document.onclick = function(){ 
        close_box(); 
        if(!fired){ 
         document.getElementById("search-input").onmouseenter = function(){ 
         show_box_fadeIn() 
         delete this.onmouseenter;}; 
      }; 
     } 
     document.getElementById("search-input").onmouseleave = function(){ 
      var fired = true; 
      if(fired){ 
       document.getElementById("search-input").onmouseenter = function(){ 
       show_box()}; 
     }; 
    } 

    document.getElementById("search-input").onclick = function(e){ 
     if(!e) { 
      e = window.event; 
     } 
     if(e.stopPropagation && e.preventDefault) { 
      e.stopPropagation(); 
      e.preventDefault(); 
     } else { 
      e.cancelBubble = true; 
      e.returnValue = false; 
     }show_box(); return true; 
    }; 

    //////////EVENTS AFTER DOCUMENT ONCLICK////////// 
    var fired = false; 
    var closeBox = false; 
    document.onclick = function(){ 
     if(!closeBox){ 
      close_box_fadeOut(); 
     } 
     if(!fired){ 
      document.getElementById("search-input").onmouseenter = function(){ 
       show_box_fadeIn() 
       delete this.onmouseenter;}; 
      }; 
     } 
     document.getElementById("search-input").onmouseleave = function() 
     { 
      var fired = true; 
      if(fired){ 
       document.getElementById("search-input").onmouseenter = function(){show_box()}; 
      }; 
     } 
     } 
     } 
     xmlhttp.open("GET","instant-search.php?keyword="+str,true); 
     xmlhttp.send(); 
     } 



    //////////FUNCTIONS////////// 
    function show_box(){ 
     document.getElementById("search-result").style.display="block"; 

    } 
    function show_box_fadeIn(){ 
     setOpacity(0); 
     document.getElementById("search-result").style.display="block"; 
     fadeIn() 
    } 
    function close_box(){ 
     document.getElementById("search-result").style.display="none"; 
    } 
    function close_box_fadeOut(){ 
     if(closeBox){ 
      document.onclick = function(){close_box();} 
      return; 
     } 
     closeBox = true;  
     setOpacity(100); 
     document.getElementById("search-result").style.display="block"; 
     fadeOut(); 
     setTimeout(close_box, 800); 
    } 
    function setOpacity(value) { 
     document.getElementById("search-result").style.opacity = value/10; 
     document.getElementById("search-result").style.filter = 'alpha(opacity=' + value * 10 + ')'; 
    } 
    function fadeIn() { 
     for(var i = 0 ; i <= 100 ; i++) 
     setTimeout('setOpacity(' + (i/10) + ')' , 10 * i); 
    } 
    function fadeOut() { 
     for(var i = 0 ; i <= 100 ; i++) 
     setTimeout('setOpacity(' + (10 - i/10) + ')' , 8 * i); 
    } 
</script> 

html代码。

<input name="keyword" type="text" size="50" id="search-input" value = 'Search'; onkeydown="showResult(this.value)" /></br></br> 

请提出任何可能的方式来做到这一点,我希望有人在那里可以帮助我。

谢谢。

+1

男人,我不会再试图重新收拾你的混乱。尝试发布更好的东西,否则你很难得到答案。另外,试着总结一下你的问题,不要发布实际工作的部分。 – MaxArt 2012-08-04 09:04:57

+0

我试着问我的问题,并得到了一个工作解决方案,但在我的代码中实现它时,它没有工作,这就是为什么我提出我的洞代码,以便更好地理解。希望你能理解。 – 2012-08-04 09:13:53

+0

我也不知道JavaScript的很多东西,我很努力的让它工作了很长时间。 – 2012-08-04 09:14:50

回答

0

终于明白了,希望这对他人也有帮助。

function showResult(str) 
{ 
if (str.length==0) 
{ 
    document.getElementById("search-result").innerHTML=""; 
    document.getElementById("search-result").style.border="0px"; 
    return; 
} 
if (window.XMLHttpRequest) 
{// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
} 
else 
{// code for IE6, IE5 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
} 
    xmlhttp.onreadystatechange=function() 
{ 
if (xmlhttp.readyState==4 && xmlhttp.status==200) 
    { 
    document.getElementById("search-result").innerHTML=xmlhttp.responseText; 
    document.getElementById("search-result").style.border="1px solid #A5ACB2"; 
    document.getElementById("search-result").autocomplete="off"; 
    document.getElementById("search-result").style.display="block"; 
    var fired = false; 
    var closeBox = false; 
    document.onclick = function() 
    { 
     if(closeBox){ 
     return; 
     }else{close_box_fadeOut();closeBox = true;} 
     if(!fired) 
     { 
    document.getElementById("search-input").onmouseenter = function(){ 
     show_box_fadeIn(); delete this.onmouseenter;}; 
     }; 
    } 
    document.getElementById("search-input").onmouseleave = function() 
    { 
     var fired = true; 
     if(fired){ 
       document.getElementById("search-input").onmouseenter = function(){show_box()}; 
       };closeBox = false; 
     } 
    document.getElementById("search-input").onclick = function(e) 
    { 
     if(!e) { 
     e = window.event; 
     } 

     if(e.stopPropagation && e.preventDefault) { 
     e.stopPropagation(); 
     e.preventDefault(); 
     } else { 
     e.cancelBubble = true; 
     e.returnValue = false; 
     }show_box(); return true; 
    } 
    } 
} 
xmlhttp.open("GET","instant-search.php?keyword="+str,true); 
xmlhttp.send(); 
} 
function show_box(){ 
    document.getElementById("search-result").style.display="block"; 
} 
function show_box_fadeIn(){ 
    setOpacity(0); 
    document.getElementById("search-result").style.display="block"; 
    fadeIn() 
} 
function close_box(){ 
    document.getElementById("search-result").style.display="none"; 
} 
function close_box_fadeOut(){ 
    setOpacity(100); 
    document.getElementById("search-result").style.display="block"; 
    fadeOut(); 
    setTimeout(close_box, 400); 
} 
function setOpacity(value) { 
document.getElementById("search-result").style.opacity = value/10; 
document.getElementById("search-result").style.filter = 'alpha(opacity=' + value * 10 + ')'; 
} 
function fadeIn() { 
for(var i = 10 ; i <= 100 ; i++) 
    setTimeout('setOpacity(' + (i/5) + ')' , 5 * i); 
} 
function fadeOut() { 
for(var i = 10 ; i <= 100 ; i++) 
    setTimeout('setOpacity(' + (6 - i/6) + ')' , 6 * i); 
}