如何动态创建推文按钮?
我目前正在尝试动态创建具有水平计数功能的推文按钮:
JavaScript
var twitter = document.createElement('a');
twitter.setAttribute('href', 'http://twitter.com/share');
twitter.setAttribute('class', 'twitter-share-button twitter-tweet');
twitter.setAttribute('data-url','http://mindcloud.co.uk/idea/?idea=' + this.id);
twitter.setAttribute('data-count', 'horizontal');
twitter.setAttribute('data-via', 'jtbrowncouk');
twitter.style.top = '20px';
twitter.style.left = '300px';
twitter.innerHTML = "Tweet";
我遇到的问题是该按钮显示为文本链接,而不是具有水平计数的按钮计数框。
我以相同的方式创建了一个 facebook 按钮,它可以正常工作,但是为了使其工作,我使用以下内容:
JavaScript
var facebook = document.createElement('fb:like');
facebook.setAttribute('id', 'like'+this.id);
facebook.setAttribute('href', 'http://mindcloud.co.uk/idea/?idea=' + this.id);
facebook.setAttribute('layout', 'button_count');
facebook.setAttribute('send', 'false');
facebook.setAttribute('width' , '300');
facebook.setAttribute('font', '');
facebook.setAttribute('show_faces', 'true');
facebook.style.top = '0px';
facebook.style.left = '300px';
使用以下内容:
FB.XFBML.parse();
解析和绘制按钮。 FB.XFBML.parse()
来自 http://connect.facebook.net/en_US/all.js
当我创建Tweet 按钮静态地位于 .html 文件内,它可以正常工作。我在我的索引页面中包含以下脚本,其中应动态创建推文按钮:
<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
如果您看到我做错了什么,请通知我!
下面的屏幕截图直观地解释了推文按钮出了什么问题!
解决方案:
我现在已经成功解决了该问题。我想象的问题是 twitter 脚本在加载时运行,并且在创建元素时不会重新运行。
使用以下 jQuery 可以正常工作!
$.getScript("http://platform.twitter.com/widgets.js");
I'm currently trying to create a tweet button with the horizontal count feature dynamically:
JavaScript
var twitter = document.createElement('a');
twitter.setAttribute('href', 'http://twitter.com/share');
twitter.setAttribute('class', 'twitter-share-button twitter-tweet');
twitter.setAttribute('data-url','http://mindcloud.co.uk/idea/?idea=' + this.id);
twitter.setAttribute('data-count', 'horizontal');
twitter.setAttribute('data-via', 'jtbrowncouk');
twitter.style.top = '20px';
twitter.style.left = '300px';
twitter.innerHTML = "Tweet";
The problem i'm having is that the button is being displayed as a text link, not as a button with the horizontal count box.
I've created a facebook button in the same way, which works correctly, however to make it work I use the following:
JavaScript
var facebook = document.createElement('fb:like');
facebook.setAttribute('id', 'like'+this.id);
facebook.setAttribute('href', 'http://mindcloud.co.uk/idea/?idea=' + this.id);
facebook.setAttribute('layout', 'button_count');
facebook.setAttribute('send', 'false');
facebook.setAttribute('width' , '300');
facebook.setAttribute('font', '');
facebook.setAttribute('show_faces', 'true');
facebook.style.top = '0px';
facebook.style.left = '300px';
using the following:
FB.XFBML.parse();
to parse and draw the button. FB.XFBML.parse()
comes from
http://connect.facebook.net/en_US/all.js
When I create the Tweet button statically inside a .html file it works correctly. I'm including the following script within my index page where the tweet button should be created dynamically:
<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
If you can see what i'm doing incorrectly please inform me!
The following screenshot gives a visual explanation to what is going wrong with the tweet button!
Solution:
I've now managed to solve the problem. The problem i'd imagine is that the twitter script is running on load, and not being re-run upon creating the element.
using the following jQuery works correctly!
$.getScript("http://platform.twitter.com/widgets.js");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
值得注意的是,最近您可以使用以下 twitter 小部件功能:
假设您已加载 widgets.js 文件:
这将为您动态地重新创建 tweet 按钮。
It's worth noting that as of recently you can use the following twitter widget function:
Assuming you've loaded the widgets.js file:
This will dynamically re-create the tweet button for you.
我现在已经成功解决了这个问题。我想象的问题是 twitter 脚本在加载时运行,并且在创建元素时不会重新运行。
使用以下 jQuery 可以正常工作!
I've now managed to solve the problem. The problem i'd imagine is that the twitter script is running on load, and not being re-run upon creating the element.
using the following jQuery works correctly!
您只是使用了错误的代码。要指定 URL,请使用 url,而不是 data-url。这是有效的:
有关更多详细信息,请查看 Twitter 的文档 https://dev.twitter.com /docs/tweet-button#properties
You're just using the wrong code. To specify the URL, you use url, not data-url. This works:
For more details, check out Twitter's documentation at https://dev.twitter.com/docs/tweet-button#properties