0

我正在使用以下代码,它完成了我想要它做的 3 件事中的 2 件事,与自定义 Twitter 按钮共享动态内容:

<script type="text/javascript">
            // <![CDATA[
            var twtTitle = document.title;
            var twtUrl = location.href;
            var maxLength = 140 - (twtUrl.length + 1);
            if (twtTitle.length > maxLength) {
                twtTitle = twtTitle.substr(0, (maxLength - 3)) + '...';
            }
            var twtLink = 'http://twitter.com/home?status=' + encodeURIComponent(twtTitle + ' ' + twtUrl);
            document.write('<a href="' + twtLink + '" target="_blank"' + '><img src="images/twitter.png"  border="0" alt="Tweet This!" /' + '><' + '/a>');
            // ]]>

        </script>

我希望它做的也是在窗口中弹出而不是全页视图。我对脚本的了解有限,所以我不知道在哪里插入适当的弹出代码。

有什么帮助吗?

4

2 回答 2

2

而不是 document.write 你需要 window.open。确保事件在单击操作中完成,否则弹出窗口阻止程序会停止您的脚本

<script type="text/javascript">
function fbs_click() {
    var twtTitle = document.title;
    var twtUrl = location.href;
    var maxLength = 140 - (twtUrl.length + 1);
    if (twtTitle.length > maxLength) {
        twtTitle = twtTitle.substr(0, (maxLength - 3)) + '...';
    }
    var twtLink = 'http://twitter.com/home?status=' + encodeURIComponent(twtTitle + ' ' + twtUrl);
    window.open(twtLink);
}
</script>

在您的 HTML 中添加您的图像标签,如下所示:

<a href="#" onclick="fbs_click();" ><img src="images/twitter.png"  border="0" alt="Tweet This!"></a>

希望这可以帮助

于 2012-10-11T05:45:52.623 回答
0

您可以在锚点上放置一个 ID="twitPop" 并使用它。

$('#twitPop').click(function(event) {
    var width  = 575,
    height = 400,
    left   = ($(window).width()  - width)  / 2,
    top    = ($(window).height() - height) / 2,
    url    = this.href,
    opts   = 'status=1' +
             ',width='  + width  +
             ',height=' + height +
             ',top='    + top    +
             ',left='   + left;
    window.open(url, 'twitter', opts);
    return false;
});
于 2015-05-19T13:35:56.037 回答