2011-12-21 27 views
2

我正在構建基於滑塊的圖片庫,並通過JavaScript加載新圖片,並且要求通過javascript設置Google +1按鈕的網址,因此可以在每個圖片上設置+1 。使用javascript設置Google +1按鈕網址

我還需要重新加載基於每個圖像的+1按鈕(URL將有一個查詢字符串參數包含圖像ID,使其唯一)。這可能嗎?

回答

5
<!-- Place this tag where you want the +1 button to render --> 
<div class="g-plusone" data-annotation="inline" data-href="#"></div> 

<!-- Place this render call where appropriate --> 
<script type="text/javascript"> 
(function() { 
    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true; 
    po.src = 'https://apis.google.com/js/plusone.js'; 
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s); 
})(); 

您可以使用下面的代碼來改變使用jQuery的gplus href屬性。首先要確保包括jQuery庫,然後使用下面的代碼:

<script type="text/javascript"> 
    jQuery(".your-button").click(function(){ 
     jQuery(".g-plusone").attr("data-href","Your-Value-Goes-Here"); 
    }); 
</script> 
1

寫在head標籤:

// Load +1 script 
(function() { 
    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true; 
    po.src = 'https://apis.google.com/js/plusone.js'; 
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s); 
})(); 

的HTML代碼:

<div id="plusone_container"> 

</div> 

,隨時隨地寫這篇文章的jQuery你想設置+1按鈕的網址:

$("#plusone_container").html('<div id="plusone"></div>'); 
    gapi.plusone.render("plusone", { "href": "Your URL" });