2011-12-21 4 views
2

スライダーベースの画像ギャラリーを構築していて、JavaScriptを使用して新しい画像を読み込み、Google +1ボタンのURLをjavascriptで設定する必要があるため、各画像に+1を設定できます。Google +1ボタンのURLをjavascriptで設定する

各画像に基づいて+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のを使用してジープラスhref属性を変更するには、次のコードを使用することができます。最初のjQueryライブラリが含まれ、その後、以下のコードを使用してください:

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

このヘッドタグでの書き込み:

// 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ボタンのURLを設定する:

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