捕获Twitter的点击“Tweet”使用jQuery的按钮 [英] capture the click of the Twitter "Tweet" button using jQuery
问题描述
我在我的网站上有以下代码,当它访问Twitter的API时会生成它自己的标记。
I have the following code on my site, which when it accesses Twitter's API generates it's own markup.
我想在<...上调用JavaScript函数code> onClick 即便如此,但随着标记的改变,似乎并不像在锚点上添加 onClick
那么简单。
I'm looking to call a JavaScript function on the onClick
even of this but, but as the markup changes it seems it's not as simple as just adding an onClick
to the anchor.
如何捕获推文按钮的点击?
How can I capture the click of the 'Tweet' button?
<div class="social_button twitter_button">
<a href="https://twitter.com/share" class="twitter-share-button" data-count="horizontal">
Tweet
</a>
<script type="text/javascript" src="//platform.twitter.com/widgets.js"></script>
</div>
推荐答案
您无法处理此元素上的点击事件,因为它在iFrame中,但是twitter让你有机会将事件绑定到按钮。
You can't handle click events on this element, because it's in an iFrame, however twitter gives you the opportunity to bind events to the button.
示例: http://jsfiddle.net/ZwHBf/2/
文档: https://dev.twitter.com/docs/intents/events
代码:
HTML :
Code: HTML:
<div class="social_button twitter_button">
<div id="foo">
<a href="https://google.com" class="twitter-share-button" data-count="horizontal">
Tweet
</a>
</div>
<script type="text/javascript" src="//platform.twitter.com/widgets.js"></script>
<script type="text/javascript" charset="utf-8">
window.twttr = (function (d, s, id) {
var t, js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s);
js.id = id;
js.src = "//platform.twitter.com/widgets.js";
fjs.parentNode.insertBefore(js, fjs);
return window.twttr || (t = {
_e: [],
ready: function (f) {
t._e.push(f)
}
});
}(document, "script", "twitter-wjs"));
</script>
</div>
JS:
twttr.events.bind('click', function(event) {
console.log('clicked');
});
这篇关于捕获Twitter的点击“Tweet”使用jQuery的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!