WordPress技巧:将选中的文字转发到新浪、腾讯微博

时间:12-05-30 栏目:wordpress 作者:炫酷网站设计建设 评论:0 点击: 775 次

由于订阅了万戈大大的博客,所以老早就看到了 WordPress 将选中文字转发到微博 这篇文章,刚才又去溜达了一下,收藏文章备用,顺便分享吧。

要实现 将选中的文字转发到新浪微博、腾讯微博 的方法很简单,只需要两步:

1、引入 jQuery,相信大多数 WordPress 博客都已经引入了 jQuery(倡萌的Hcms主题默认已加载),那就可以直接进行第二步了。

2、在页面底部,或者更确切的说,在引入 jQuery 库的后面加上这样一段 JS即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
var miniBlogShare = function() {
    //指定位置驻入节点
    $('<img id="imgSinaShare" title="将选中内容分享到新浪微博" src="http://wange.im/wp-content/themes/wange/images/sina_share.gif" /><img id="imgQqShare" title="将选中内容分享到腾讯微博" src="http://wange.im/wp-content/themes/wange/images/tt_share.png" />').appendTo('body');

    //默认样式
    $('.img_share').css({
        display : 'none',
        position : 'absolute',
        cursor : 'pointer'
    });

    //选中文字
    var funGetSelectTxt = function() {
        var txt = '';
        if(document.selection) {
            txt = document.selection.createRange().text;
        } else {
            txt = document.getSelection();
        }
        return txt.toString();
    };

    //选中文字后显示微博图标
    $('html,body').mouseup(function(e) {
        if (e.target.id == 'imgSinaShare' || e.target.id == 'imgQqShare') {
            return
        }
        e = e || window.event;
        var txt = funGetSelectTxt(),
            sh = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0,
            left = (e.clientX - 40 < 0) ? e.clientX + 20 : e.clientX - 40,
            top = (e.clientY - 40 < 0) ? e.clientY + sh + 20 : e.clientY + sh - 40;
        if (txt) {
            $('#imgSinaShare').css({
                display : 'inline',
                left : left,
                top : top
            });
            $('#imgQqShare').css({
                display : 'inline',
                left : left + 30,
                top : top
            });
        } else {
            $('#imgSinaShare').css('display', 'none');
            $('#imgQqShare').css('display', 'none');
        }
    });

    //点击新浪微博
    $('#imgSinaShare').click(function() {
        var txt = funGetSelectTxt(), title = $('title').html();
        if (txt) {
            window.open('http://v.t.sina.com.cn/share/share.php?title=' + txt + ' —— 转载自:' + title + '&url=' + window.location.href);
        }
    });

    //点击腾讯微博
    $('#imgQqShare').click(function() {
        var txt = funGetSelectTxt(), title = $('title').html();
        if (txt) {
            window.open('http://v.t.qq.com/share/share.php?title=' + encodeURIComponent(txt + ' —— 转载自:' + title) + '&url=' + window.location.href);
        }
    });
}();

上面的代码调用的图片来自万戈大大的网站,希望大家下载图片上传到自己的空间,为万戈大大省点流量,也防止图片失效。>>图标下载




这篇日志的二维码,扫描一下进行收藏:

WordPress技巧:将选中的文字转发到新浪、腾讯微博:等您坐沙发呢!

发表评论

您必须 [ 登录 ] 才能发表留言!

------====== 本站公告 ======------
专业网站设计制作

读者排行

最新评论