Domanda

come posso cambiare l'attributo href del mio pulsante Google +1 attraverso qualche jQuery

Il tasto inizialmente carichi con un href vuoto.

qui è quello che ho provato finora

$(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>
È stato utile?

Soluzione 2

Finalmente ho a lavorare il modo in cui ho voluto che. Ho ristrutturato il codice un po 'per farlo funzionare. Invece di cercare di cambiare l'attributo href su document.ready, ho finito per creare un div vuoto per essere il contenitore per il Google Plus tasto e scrivere il pulsante con il corretto href via .html ()

//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>

Altri suggerimenti

Usa il JSAPI a esplicitamente rendere il + 1 pulsante all'interno della funzione jQuery dopo aver impostato il href.

Select the tag using jquery's ".next()" function. You just need to put a selectable tag just before this button.

Why do you want to do this?

You wouldn't need to have the HTML in a JS string.

I created a page similar where I created a div and inside of it I put my g:plusone tag. I put the URL as an attribute in the div as "data-url", but you wouldn't have to do that here.

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

Then I am calling gapi.plusone.render and passing in the URL:

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

I have used the following code which works correctly:

<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>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top