我正在尝试创建自定义共享链接,因此单击它将共享当前 URL。 我明白这一点
<a href="http://twitter.com/share?text=An%20Awesome%20Link&url=http://www.google.com">
Share This on Twitter</a>
但是有没有办法让它变得动态,这样它就会抓取用户所在页面的 URL 并共享它,而不是硬编码的链接。
谢谢
最佳答案
在 Javascript 中你可以使用:
document.URL
它给你当前的网址
https://developer.mozilla.org/en-US/docs/DOM/document.URL
和
document.title
用于获取此页面的标题
https://developer.mozilla.org/en-US/docs/DOM/document.title
使用 jQuery 或 javascript,您可以设置 href 属性
http://docs.jquery.com/Attributes/attr
https://developer.mozilla.org/en-US/docs/DOM/stylesheet/href
示例:
$('a').on('click', function() {
$(this).attr('href',
'http://twitter.com/share?text='+document.title+'&url=' + document.URL);
});
另一种方式:
关于html - Twitter/Facebook 分享动态 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15656695/