2016-06-09 16 views
-1

我試圖在一個鼠標懸停的標記內部打開一個可點擊的鏈接,並讓它在我的地圖上流動。我到目前爲止的代碼不會將任何數據屬性傳遞給span標籤。如果我把除link_to之外的任何東西放在area.name中,它就會起作用。然而,任何與link_to和什麼都不會發生。我試着谷歌周圍,只是發現如何把data_ attribute到link_to助手,而不是其他的方式。將<%= link_to%>放入數據屬性中

HTML:

<div class="map"> 
    <%= image_tag("strangemap.png", :usemap => "#worldmap", :class => "mapper") %> 
    <map name="worldmap"> 
    <% @areas.each do |area| %> 
     <area class="target noborder" 
      shape="poly" coords="<%= area.coords %>" 
      data-bottom="<%= link_to area.name, area_path(area) %>"> 
    <% end %> 
    <span id="boxbottom"></span> 
    </map> 
</div> 

的jQuery:

$(document).ready(function(){ 
    $("area").mouseover(function(){ 
    $("#boxbottom").fadeIn(0); 
    document.getElementById("boxbottom").innerHTML = $(this).data('bottom'); 
    }); 
}); 
+0

移動跨度出 ...不是一個有效的小孩 – charlietfl

+0

''標籤缺少一個右括號。 – Substantial

+0

'getElementById(「boxbuttom」)'包含一個重要的錯字。 – Substantial

回答

1

好吧,我發現周圍的工作這一點。

而不是嘗試傳遞範圍內的link_to,我分別傳遞路徑「area_path(area)」和名稱「area.name」。

<div class="map"> 
    <%= image_tag("strangemap.png", :usemap => "#worldmap", :class => "mapper") %> 
    <map name="worldmap"> 
    <% @areas.each do |area| %> 
     <area class="target noborder" 
      shape="poly" coords="<%= area.coords %>" 
      data-path="<%= area_path(area) %> 
      data-name="<%= area.name %>"> 
    <% end %> 
    <span id="boxbottom"></span> 
    </map> 
</div> 

劇本里面,我下面的代碼添加到它

$(document).ready(function(){ 
    $("area").mouseover(function(){ 
    $("#boxbottom").fadeIn(0); 
    document.getElementById("boxbottom").innerHTML = $(this).data('name'); 
    var path = $(this).data('path'); 
    $("#boxbottom").click(function(){ 
     window.location = path; 
    }); 
    }); 
}); 
相關問題