質問

jQueryを介してGoogle +1ボタンのhref属性を変更するにはどうすればよいですか

ボタンは最初に空の href をロードします。

これが私がこれまで試したことです

$(document).ready(function () {
    var qrCode = 'A12345';
    var shareLink = "http://<?php echo $_SERVER['HTTP_HOST'];?>/show.php?qrCode="+qrCode;
    $("#shareLink").attr("href", shareLink);
});

<g:plusone size='medium' id='shareLink' annotation='none' href='' callback='countGoogleShares'></g:plusone>
役に立ちましたか?

解決 2

ようやく希望どおりに動作するようになりました。動作させるためにコードを少し再構成しました。document.ready の href 属性を変更しようとする代わりに、Google Plus ボタンのコンテナとなる空の div を作成し、.html() を介して正しい href を含むボタンを書き込むことになりました。

//load the google plus javascript api
<script type="text/javascript" src="https://apis.google.com/js/plusone.js">
    {"parsetags": "explicit"}
</script>

//create an empty div to be the target of the google plus button
<div id="plusContent"></div>

<script>
    //get the qrcode to append to the url (ajax call in real life)
    var qrCode = 'A12345';

    //build the google plus object text string
    var googleShareLink = "<g:plusone size='medium' id='shareLink' class='shareLink'  annotation='none' href='http://<?php echo $_SERVER['HTTP_HOST'];?>/show.php?qrCode="+qrCode+"' callback='countGoogleShares'></g:plusone>";

    //write the google plus object to the target div
    $("#plusContent").html(googleShareLink);

    //render the +1 buttons in the target location
    gapi.plusone.go("plusContent");
</script>

他のヒント

JSAPIを使用してください 明示的にレンダリング hrefを設定した後のjquery関数内の+1ボタン。

jQueryの「.next()」関数を使用してタグを選択します。このボタンの直前に選択可能なタグを配置する必要があります。

なぜこれをしたいのですか?

JS文字列にHTMLを使用する必要はありません。

私が作成しました ページ 同様の場所で、Divを作成し、その内部にG:Plusoneタグを付けました。 URLをdivの属性として「data-url」として配置しますが、ここでそれを行う必要はありません。

<div class="plusoneContent" data-url="http://www.adamkoch.com/2012/05/04/moz-proxy-issues/">
<g:plusone></g:plusone>
</div>

それから私はgapi.plusone.renderを呼び出して、URLを通過します:

var url = $(this).data('url');
gapi.plusone.render(this, {href: url});

正しく動作する次のコードを使用しました。

<script type="text/javascript" src="http://apis.google.com/js/plusone.js">
</script>
<div id="plusone-div" class="plusone"></div>
<script type="text/javascript">
    gapi.plusone.render('plusone-div',{"size": "medium", "count": "true", "href": "[http://MySite.Com/]"});
</script>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top