如何动态创建推文按钮?

发布于 2024-11-28 13:57:49 字数 1953 浏览 6 评论 0原文

我目前正在尝试动态创建具有水平计数功能的推文按钮:

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>

如果您看到我做错了什么,请通知我!

下面的屏幕截图直观地解释了推文按钮出了什么问题! Tweet 按钮未正确显示。

解决方案:

我现在已经成功解决了该问题。我想象的问题是 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!
Tweet button not displaying correctly.

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

长亭外,古道边 2024-12-05 13:57:49

值得注意的是,最近您可以使用以下 twitter 小部件功能:

twttr.widgets.load();

假设您已加载 widgets.js 文件:

<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>

这将为您动态地重新创建 tweet 按钮。

It's worth noting that as of recently you can use the following twitter widget function:

twttr.widgets.load();

Assuming you've loaded the widgets.js file:

<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>

This will dynamically re-create the tweet button for you.

提笔书几行 2024-12-05 13:57:49

我现在已经成功解决了这个问题。我想象的问题是 twitter 脚本在加载时运行,并且在创建元素时不会重新运行。

使用以下 jQuery 可以正常工作!

$.getScript("http://platform.twitter.com/widgets.js");

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");
往昔成烟 2024-12-05 13:57:49

您只是使用了错误的代码。要指定 URL,请使用 url,而不是 data-url。这是有效的:

var twitter = document.createElement('a');
twitter.setAttribute('href', 'http://twitter.com/share');
twitter.setAttribute('class', 'twitter-share-button twitter-tweet');
twitter.setAttribute('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";

有关更多详细信息,请查看 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:

var twitter = document.createElement('a');
twitter.setAttribute('href', 'http://twitter.com/share');
twitter.setAttribute('class', 'twitter-share-button twitter-tweet');
twitter.setAttribute('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";

For more details, check out Twitter's documentation at https://dev.twitter.com/docs/tweet-button#properties

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文