2011-01-12 62 views
0

http://geodit.com:8000/test如何使用javascript模擬鼠標點擊(在Google地圖中)?

在這裏,我有一個簡單的Google本地搜索API和Google地圖代碼。

我該如何做到這一點,以便當我點擊搜索結果的標題時,我模擬點擊地址?我的目標是通過單擊標題使彈出框出現在地圖上。我綁定到這:

$(".gs-title").click(function(){ 

    //SIMULATE MOUSE CLICK of the address HERE 
    $(this).parent().parent().find(".gs-address").click(); // this doesn't work!! 

    alert("the title was clicked!!"); //this works fine. 
    return false; //this also works fine. The hyperlink gets disabled because I returned false. 

}); 

我希望能夠點擊標題,而不是打開鏈接。然後,彈出地圖上的框。換句話說,我想把整個結果看待。

+0

你確定`$(this).parent()。parent()。find(「.gs-address」)`實際上是在找到你認爲它的結果嗎? – Pointy 2011-01-12 19:02:11

+0

我確實看到它打開彈出罰款,當我點擊結果。但我也看到彈出窗口打開。如果我只是點擊地址區域而不是標題,那麼它就是你想要的。我錯過了什麼? – spinon 2011-01-12 19:03:17

+0

我希望能夠點擊標題,而不是打開鏈接。然後,彈出地圖上的框。換句話說,我想把整個結果看待。 – TIMEX 2011-01-12 19:11:03

回答

0

的HTML代碼是:

<div class="gs-title"><a target="_blank" class="gs-title" href="http://www.google.com/maps/place?source=uds&amp;q=pizza&amp;cid=12855512876323852687">California <b>Pizza</b> Kitchen</a></div> 

    <div class="gs-address">... 

所以我認爲正確的道路coul'd是:

$("a.gs-title").click(function(){ 

$(this).parent().siblings('.gs-address').click();return false; 
}); 
1

解決。我手動刪除了A標籤。

// Returns the HTML we display for a result before it has been "saved" 
    LocalResult.prototype.html = function() { 
     var me = this; 
     var container = document.createElement("div"); 
     container.className = "unselected"; 
     container.appendChild(me.result_.html.cloneNode(true)); 
     $(container).find('a').replaceWith(function() { return $(this).contents(); }); 

     return container; 
    }