【发布时间】:2016-12-17 03:04:56
【问题描述】:
我正在研究一种在单击显示新内容后隐藏 twitter 按钮的方法,我以为我可以使用它,但它仍然没有。我不知道为什么如果有人能指出我的错误会有所帮助。 谢谢。
----更新----
当我添加这个脚本时它停止工作
<script type="text/javascript" src="//platform.twitter.com/widgets.js"></script>
和
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
----更新结束----
代码如下:
(function($) {
var win = null;
$.fn.tweetAction = function(options, callback) {
// Default parameters of the tweet popup:
options = $.extend({
url: window.location.href
}, options);
return this.click(function(e) {
if (win) {
// If a popup window is already shown,
// do nothing;
e.preventDefault();
return;
}
var width = 550,
height = 350,
top = (window.screen.height - height) / 2,
left = (window.screen.width - width) / 2;
var config = [
'scrollbars=yes', 'resizable=yes', 'toolbar=no', 'location=yes',
'width=' + width, 'height=' + height, 'left=' + left, 'top=' + top
].join(',');
// Opening a popup window pointing to the twitter intent API:
win = window.open('http://twitter.com/intent/tweet?' + $.param(options),
'TweetWindow', config);
// Checking whether the window is closed every 100 milliseconds.
(function checkWindow() {
try {
// Opera raises a security exception, so we
// need to put this code in a try/catch:
if (!win || win.closed) {
throw "Closed!";
} else {
setTimeout(checkWindow, 100);
}
} catch (e) {
// Executing the callback, passed
// as an argument to the plugin.
win = null;
callback();
}
})();
e.preventDefault();
});
};
})(jQuery);
$(document).ready(function() {
$('#tweetLink').tweetAction({
text: 'First tweet',
url: '#',
via: 'website'
}, function() {
$('hidden-text')
.show();
$(".hidden-text").removeClass("hidden-text");
});
});
$(document).ready(function() {
$("p").click(function() {
$(this).hide("slow");
});
});
.hidden-text {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>Tweet to show content.
<a href="https://twitter.com/intent/tweet?screen_name=your_twitter" class="twitter-mention-button" id="tweetLink" data-show-count="false">Tweet to @your_twitter</a>
</p>
<p class="hidden-text">thank you for tweeting !</p>
更新 优化了一些js
$(document).ready(function () {
$('#tweetLink').tweetAction({
text: 'First tweet',
url: '#',
via: 'website'
}, function () {
$('hidden-text')
.show("slow");
$(".hidden-text").removeClass("hidden-text");
$("#hide-me").hide("slow");
});
});
更新 3
好的,看看控制台,我似乎遇到了错误
Uncaught TypeError: $(...).tweetAction is not a function
我已经更新了我的 Jquery CDN,当我点击推文到 @your_twitter 时,它现在不会在新窗口中打开,而是在同一窗口中更改 url。有什么想法吗?
已解决
我不得不删除一些 CDN
<link href="https://get.gridsetapp.com/35679/" rel="stylesheet"/>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.0/themes/base/jquery-ui.css">
<!--<script src="https://code.jquery.com/jquery-1.12.4.js"></script> -->
<!--<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js"></script>-->
<!--<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>-->
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>
<!--<script src="https://code.jquery.com/jquery-3.1.0.min.js" ></script>-->
【问题讨论】:
-
我编辑了您的问题,将您的代码设置为 sn-p,并且它似乎可以正常工作...您测试过不同的浏览器吗?
-
嗯,我在最新的 chrome 上,会再试一次。谢谢
-
啊不,我发现了问题,请查看更新
标签: javascript jquery html css twitter