百度和優酷的搜尋體驗改善,類似Google Instant搜尋

weixin_34391854發表於2011-06-29

效果檢視

firefox和ie可用,oepra有少許問題!

上次看到國外那誰誰實現了Youtube的Instant搜尋,據說花了三小時。

我笑了,我看了那篇文章後,自己花了三小時,實現了百度和優酷的Instant搜尋體驗。

難點在於,鍵盤輸入的監聽。用了JQuery自帶的keyup函式,再加JS自帶的setTimeout函式,較好地實現了效果。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    
<title></title>
    
<style type="text/css">
    .hidden
    
{
        display
:none;
    
}
    .show
    
{
        display
:block;
         border
:0;
    
}
    #up
    
{
        margin
:-100px 0 0 0;
        width
:1300px;
        height
:900px;
        
        
/* text-align:center;  */
    
}
    
        #getKey
        
{
            height
: 36px;
            width
: 379px;
            font-size
:large;
            
        
}
    
    
</style>
    
<script src="JS/jquery-1.4.2-vsdoc.js" type="text/javascript"></script>
    
<script type="text/javascript">
        
var baidu = 1var youku = 0;
        
var getKey; 
        
function gotothere() {
            getKey 
= $("#getKey").val();
            
if (getKey == "") {
                
//alert("sorry");
                //$("up").className = "show";
            }
            
else {
                
if (baidu == 1) {
                    $(
"#up").attr("src""http://www.baidu.com/s?wd=" + encodeURI(getKey) + "&inputT=624&ie=utf-8");
                }
                
else if (youku == 1) {
                    $(
"#up").attr("src""http://www.soku.com/search_video/q_" + getKey);
                }
            }
        }
        $(
function() {
            $(
"#getKey").keyup(function() {//監聽鍵盤按下事件
                
                
var timer = setTimeout("gotothere()"500);//有時候鍵盤按得太快,監聽事件來不及反應,所以用setTimeout函式來延遲
            });


        });

        $(
function() {
            $(
"#baidu").click(function() {
                baidu 
= 1; youku = 0;
                $(
"#up").attr("src""http://www.baidu.com/s?wd=" + encodeURI(getKey) + "&inputT=624&ie=utf-8");
            });
            $(
"#youku").click(function() {
                youku 
= 1; baidu = 0;
                $(
"#up").attr("src""http://www.soku.com/search_video/q_" + getKey);
            })
        })

    
</script>
</head>
<body style="overflow-y: hidden;">
請輸入你想要搜尋的關鍵字:
<input id="getKey" type="text" /><href="#" id="baidu">百度</a>&nbsp|&nbsp<href="#" id="youku">優酷</a><br />
<div style="overflow:hidden"><iframe id="up" class="show" frameborder="0" ></iframe></div>
</body>
</html>

 

相關文章