Pregunta

¿Cómo puedo cambiar el atributo href de mi botón Google +1 a través de un jQuery?

El botón inicialmente se carga con un href vacío.

Esto es lo que he probado hasta ahora

$(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>
¿Fue útil?

Solución 2

Finalmente lo hice para funcionar como quería. Reestructuré un poco el código para que funcione. En lugar de tratar de cambiar el atributo href en documento. Listo, terminé creando un div vacío para ser el contenedor para el botón Google Plus y escribir el botón con el href correcto a través de .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>

Otros consejos

Usa el JSAPI para renderizar explícitamente El botón +1 dentro de la función jQuery después de configurar el href.

Seleccione la etiqueta usando la función de jQuery ".next ()". Solo necesita poner una etiqueta seleccionable justo antes de este botón.

¿Por qué quieres hacer esto?

No necesitaría tener el HTML en una cadena JS.

yo creé una página similar donde creé un div y dentro de él puse mi etiqueta G: Plusone. Puse la URL como un atributo en el DIV como "IRL de datos", pero no tendrías que hacerlo aquí.

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

Luego llamo a Gapi.plusone.render y pasando la URL:

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

He usado el siguiente código que funciona correctamente:

<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>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top