如何将文本框内容分享到Twitter?

3

我需要通过Twitter分享。我有一个js函数quotes(),它会生成一条随机引语和以下HTML:

<textarea id="txtbox" style="width:600px; height: 50px;" ></textarea>

 <button onClick="quotes()">Click Here</button>

下面我需要一个 Twitter 分享按钮,就像这样:
<a href="https://twitter.com/share" class="twitter-share-button"{count} data-url="goo.gl/udj2qQ" data-text= '' >Tweet</a>

但在 data-text 部分,我需要引用我的语录。

你能添加“引号”功能的内容吗?你能更明确地说明你想要什么吗? - Romain
1个回答

1
解决方案是删除旧按钮,创建一个新按钮,并再次运行tweeter sdk代码,使用twttr.widgets.load(); 更多信息:

function quotes() {
  var txt = document.querySelector('textarea').value;
  var tbutton = document.querySelector('.twitter-share-button');
  
  tbutton.parentNode.removeChild(tbutton);
  
  var newA = document.createElement('a');
  newA.setAttribute('href', 'https://twitter.com/share');
  newA.setAttribute('class', 'twitter-share-button');
  newA.setAttribute('data-url', 'goo.gl/udj2qQ');
  newA.setAttribute('data-text', txt);
  document.body.appendChild(newA);
  twttr.widgets.load();
}
<textarea id="txtbox" style="width:600px; height: 50px;"></textarea><br />
<button onClick="quotes()">Click Here</button>
<hr />
<a href="https://twitter.com/share" class="twitter-share-button"{count} data-url="goo.gl/udj2qQ" data-text="blabla">Tweet</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>

http://jsbin.com/wonasi


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接