0

我有顯示圖像的酥料餅: 我的html:引導3酥料餅

%a#btn2{"data-content" => "#{ image_tag CarConfiguration.last.image_url.to_s}".html_safe ,"data-placement" => "bottom"} Click to toggle popover 

我的JS:

$('#btn2').popover trigger: "hover", html: true 

我想這個片段導出到我的JS: "data-content" => "#{ image_tag CarConfiguration.last.image_url.to_s}"

我該怎麼辦?

回答

1

您可以在定義彈出窗口的js函數中設置內容。

這將是這樣的:

var myPopoverContent = "#{ image_tag CarConfiguration.last.image_url.to_s}".html_safe; 

$('#btn2').popover({ 
    trigger: 'hover', 
    html: true, 
    content: myPopoverContent 

}); 

http://jsfiddle.net/afolkesson/s9L9A/

+0

感謝的求助。但是這個代碼不能正常工作。問題出在:var myPopoverContent =「#{image_tag CarConfiguration.last.image_url.to_s}」。html_safe; 它沒有加載這張照片。我不知道如何在javascript中評估ruby代碼。 –

+0

我不熟悉ruby-on-rails,但是如果你可以將它作爲數據內容標籤返回,那麼你可以添加一個隱藏的輸入並將該值設置爲url。如下所示: %a#hiddenInput {「value」=>「#{image_tag CarConfiguration.last.image_url.to_s}」。html_safe} 然後,您可以像這樣檢索js代碼中的值: var myPopoverContent = $( 「#hiddenInput」)。VAL() –