WordPress 将选中文字转发到微博
时间:2011-11-03 17:29:33
最近在看腾讯新闻的时候,无意中发现,当我选中新闻中的文字的时候,鼠标右上角会显示一个“转播至微博”的按钮,点击后就会将选中的文字转发到微博上。这是一个很不错的用户体验,如果能把它引入到 WordPress 博客中,那不是很好吗?
为此我还特地去注册了一个腾讯微博开放平台的开发者,当我开始阅读开发文档的时候,才发现,他妹的,腾讯官方已经推出一个相同功能的应用,叫作 “Q-Share”,再翻阅了一下其他资料,原来已经有前辈写出了js 页面文字选中后分享到新浪微博的方法,那我就省力多了,两者结合一下,把新浪微博和腾讯微博两个按钮都加上了,然后闲的蛋疼的我又把它翻译成了 jQuery 的方法。
效果的话就可以看本站了,选中任何文字,就会在右上角显示两个微博按钮,点击试试吧。
实现的方法也很简单,只需要两步:
1、引入 jQuery,相信大多数 WordPress 博客都已经引入了 jQuery,那就可以直接进行第二步了。
2、在页面底部,或者更确切的说,在引入 jQuery 库的后面加上这样一段 JS,你就可以看到和本站一样的效果了。
var miniBlogShare = function() {
//指定位置驻入节点
$('<img id="imgSinaShare" class="img_share" title="将选中内容分享到新浪微博" src="https://img.aspxhome.com/file/UploadPic/201111/3/1320285750869-10.gif" /><img id="imgQqShare" class="img_share" title="将选中内容分享到腾讯微博" src="https://img.aspxhome.com/file/UploadPic/201111/3/1320285750811-32.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);
}
});
}();
是不是很简单呀?
赶紧选中本文的标题,在微博上通知你的好友来围观吧,哈哈~
PS:最后,说一句,其实这个方法不仅仅适用于WordPress,JS 是通用的,你可以把这段JS 挂载到任何网站上都可以得到相同的效果。
PS2:最最最后,再说一句,麻烦各位在使用前,把JS 里的两张微博的图片路径替换成自己的地址,因为小站流量有限,大家帮我省着点花呗,嘿嘿。
转载请注明来源:http://wange.im/wordpress-share-selected-text-to-weibo.html
标签:腾讯,微博,wordpress
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
帝国CMS与PHPCMS对比(四):碎片功能分析
2011-08-26 12:57:07
![](https://img.aspxhome.com/file/UploadPic/20118/26/1b2c0f01010p0000g92-30s.gif)
快速配置Vsftp的方法详细介绍
2009-12-07 18:56:00
网站访问流量的主要统计指标
2009-01-04 10:35:00
展现方式成为搜索战争的突围口?
2007-10-15 18:41:00
![](https://img.aspxhome.com/file/UploadPic/200710/15/2007101518484923s.gif)
AdSense-中文:链接架构的重要性
2008-12-10 14:55:00
Win2008系统活动目录权限管理服务
2009-11-16 13:34:00
令网站提速的7大秘方网站
2007-09-19 13:45:00
解读百度首份《搜索引擎优化指南》
2010-08-16 21:21:00
![](https://img.aspxhome.com/file/UploadPic/20108/16/1611350-71s.jpg)
博雅立方:指引传统B2C的突围之路
2009-10-27 13:48:00
建立网站三个步骤
2007-09-21 12:08:00
UNIX下DNS服务器之管理维护篇
2009-09-20 20:31:00
Cyrus IMAP邮件服务器安装与配置(2)
2007-08-14 15:42:00
发现并防止对WEB应用服务器的三种攻击
2008-12-23 15:54:00
10条方法让网民在你的网站待的更久
2008-03-10 12:33:00
服务器管理最容易犯的十大错误
2009-07-16 14:10:00
构建反病毒反垃圾邮件系统(三)
2007-09-27 13:43:00
Godaddy主机如何绑定域名到子文件夹新建网站的说明 Godaddy
2010-04-21 12:48:00
![](https://img.aspxhome.com/file/UploadPic/20104/7/201047132147924.gif)
Exchange 2007:10大特性扫描
2010-02-26 19:06:00
Vsftpd+tcp_wrappers控制主机和用户访问
2009-09-20 20:17:00
美国主机godaddy空间使用的问题
2010-04-12 13:02:00