2013-02-10 76 views
1

我在做一個website。當你點擊我的搜索框然後輸入你點擊搜索,然後它把你帶到搜索結果的google.com如何在輸入框架時使用ajax顯示結果?當你在http://google.com上搜索你輸入的文本通過其自我而不按搜索結果我怎麼能做到這一點?這是我的html片段在我的網站上的Google自動搜索結果

<div class='search'> 
<form method="get" id='search' action="http://www.google.com/search"> 
<input type="hidden" name="q" size="31" maxlength="255" value="Site Name:" /> 
<input type="text" name="q" size="31" maxlength="255" style="height: 24px;" placeholder="Search..." /> 
</div> 

我有很多的CSS後面它使文本框放大和改變顏色。這裏是相關的代碼片段。

#search input[type="text"] { 
background: url(imgs/search-white.png) no-repeat 10px 6px #444; 
border: 0 none; 
font: bold 12px Arial,Helvetica,Sans-serif; 
color: #d7d7d7; 
width:150px; 
padding: 6px 15px 6px 35px; 
-webkit-border-radius: 20px; 
-moz-border-radius: 20px; 
border-radius: 20px; 
text-shadow: 0 2px 2px rgba(0, 0, 0, 0.3); 
-webkit-box-shadow: 0 1px 0 rgba(255, 255, 255, 0.1), 0 1px 3px rgba(0, 0, 0, 0.2) inset; 
-moz-box-shadow: 0 1px 0 rgba(255, 255, 255, 0.1), 0 1px 3px rgba(0, 0, 0, 0.2) inset; 
box-shadow: 0 1px 0 rgba(255, 255, 255, 0.1), 0 1px 3px rgba(0, 0, 0, 0.2) inset; 
-webkit-transition: all 0.7s ease 0s; 
-moz-transition: all 0.7s ease 0s; 
-o-transition: all 0.7s ease 0s; 
transition: all 0.7s ease 0s; 
outline: none; 
} 

#search input[type="text"]:focus { 
background: url(imgs/search-dark.png) no-repeat 10px 6px #fcfcfc; 
color: #6a6f75; 
width: 175px; 
-webkit-box-shadow: 0 1px 0 rgba(255, 255, 255, 0.1), 0 1px 0 rgba(0, 0, 0, 0.9) inset; 
-moz-box-shadow: 0 1px 0 rgba(255, 255, 255, 0.1), 0 1px 0 rgba(0, 0, 0, 0.9) inset; 
box-shadow: 0 1px 0 rgba(255, 255, 255, 0.1), 0 1px 0 rgba(0, 0, 0, 0.9) inset; 
text-shadow: 0 2px 3px rgba(0, 0, 0, 0.1); 
outline: none; 
} 
div.search 
{ 
position:fixed; 
top:8px; 
right:5px; 
} 

因此,如何讓我的ajax框架顯示谷歌搜索結果,你鍵入? 我什麼都不知道AJAX我不知道如何開始行與它的代碼ither所以請在​​深入解釋

*側面說明 如果對不起網站看起來壞的IM 13

回答

1

不知道的JavaScript你很好,我建議先學習它。 w3schools.com將是一個好地方。

谷歌不再讓你使用他們的網站通過iframe,所以我建議使用startpage.com作爲其結果拉從google.com

,我能想到的做到這一點最簡單的方法是這樣的(在這個例子中,我使用本網站的移動版本,因爲它嵌入更好。):http://jsfiddle.net/A8M4r/

<!DOCTYPE html> 
<html> 
<head> 
<script> 
function querify(query) { 
    return query.split(" ").join("+") // replaces spaces with +s for url 
} 
function updateIframe(query) { 
    query = querify(query); 
    var i = document.getElementById("searchResults"); 
    var searchEngine = "http://startpage.com/do/m/mobilesearch/?q=" 
    var yourSiteToSearch= "site:example.com+" 
    i.src = searchEngine + yourSiteToSearch + query; 
} 
</script> 
</head> 
<body> 
    <input oninput="updateIframe(this.value)" type="text"> 
    <iframe id="searchResults" height="100%" width="100%"> 
</body> 

希望這有助於!

P.S.如果你想要一個iframe只在用戶點擊搜索框時彈出,那麼在這裏有一個:jsfiddle.net/4EDUK

+0

非常感謝我更新了它如何使iframe顯示僅當文本框被點擊? – 2013-02-10 04:40:57

+0

可能最簡單的方法是這樣做:http://jsfiddle.net/TqhCZ/ – Stephen 2013-02-10 04:55:39

+0

哦,你可能會想提交你的網站到谷歌他們索引,它看起來不像他們已經。 http://www.google.com/webmasters/tools/submit-url – Stephen 2013-02-10 04:58:45

0
$(window).load(function() 
{ 
    var opts = 
    { 
     url: "http://www.google.com/search?q=" + $("#mysearchInput").val(), 
     success: function(data) 
     { 
      //parse the results which are in variable "data". 
      //You're going to need to analyze the results yourself 
      //and parse it yourself, in whatever way you want to 

      var myresults = "my example results here"; 
      $("#myiframe").append(myresults); 
     } 
    } 

    $.ajax(opts); 
}); 
+0

我怎麼能在我的代碼中使用這個,就像我上面說的那樣,我不知道這是如何工作的,我只知道html css和php。我會在哪裏放置它,以及我需要做些什麼才能使其工作如何我需要 – 2013-02-10 01:49:57

+0

請參閱我對您的重複問題的回覆http://stackoverflow.com/questions/14793160/how-to-search-box-auto-搜索結果/ 14793216#14793216 /。它包括所有HTML,CSS,jQuery,PHP等,你需要完成你所描述的內容。 – 2013-02-10 01:52:36

+0

@htmled我刪除那個問題,我使用了該網站,仍然無法弄清楚。 – 2013-02-10 01:54:41

相關問題