从浏览器地址栏分享(fb,gplus),URL?

时间:2017-10-18 13:08:43

标签: javascript jquery

我试图在facebook + googleplus中自动分享地址栏

我的Html + js代码:

<a class="facebook" target="_blank" href="">Facebook</a>
<a class="googleplus" target="_blank" href="">Google+</a>

<script>
document.getElementById("facebook").href = "http://www.facebook.com/sharer.php?u=" + window.location.href;
document.getElementById("googleplus").href = "http://plus.google.com/u/0/share?url=" + window.location.href;
</script>

空结果

img

如何修复此空结果

1 个答案:

答案 0 :(得分:2)

您的目标是ID,而非目标:

document.getElementById("facebook").href = "http://www.facebook.com/sharer.php?u=" + window.location.href;
document.getElementById("googleplus").href = "http://plus.google.com/u/0/share?url=" + window.location.href;
<a id="facebook" target="_blank" href="">Facebook</a>
<a id="googleplus" target="_blank" href="">Google+</a>