0

我已经使用 javascript 实现了即时搜索,除了在某一时刻之外,我都能让它工作。

我已经在即时搜索中实施了以下操作,并且工作正常。

  1. 结果来自“搜索结果”div。
  2. 单击文档结果消失的任何位置时。
  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>

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

谢谢。

4

1 回答 1

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 );
}
于 2012-08-04T11:48:15.770 回答