WordPress实用代码(WordPress将选中内容分享到新浪腾讯微博)

笔者将在实际使用WordPress博客程序的过程中,记录一些实用的WordPress代码和大家分享,今天记录的是WordPress将选中内容分享到新浪腾讯微博的代码,如果有需要的可以参考着试试。

WordPress将选中内容分享到新浪腾讯微博的方法如下:

1、引入jQuery,已经引入了jQuery,那就可以直接进行第二步了。

2、在引入jQuery库的后面加上这样一段JS,我们就可以看到效果了。

实例代码如下:

var miniBlogShare = function() {
//指定位置驻入节点
$('<img id="imgSinaShare" class="img_share" title="将选中内容分享到新浪微博" src="1328255868614.gif" /><img id="imgQqShare" class="img_share" title="将选中内容分享到腾讯微博" src="/1328255868314.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);
}
});
}();

可以看到$sinaMiniBlogShare方法有两个参数,eleShare和eleContainer,其中,前一个参数是必须的,指的是文字选中后出现的浮动层元素(在本文demo中就是新浪眼睛图标),后面一个参数指文字选择的容器元素,可选参数,如果不设置则指document元素,也就是整个页面文字选中都会触发分享的功能。
假设新浪微博分享图标的HTML如下:

<img id="imgSinaShare" class="img_sina_share" title="将选中内容分享到新浪微博" src="http://simg.sinajs.cn/blog7style/images/common/share.gif" />

则直接使用如下代码:

$sinaMiniBlogShare(document.getElementById("imgSinaShare"));

以上就是Wordpress将选中内容分享到新浪腾讯微博的简单代码分享,有需要的可以参考。

<<<<<<便宜稳定美国VPS推荐>>>>>>搬瓦工VPS年付49.99美元起

未经允许不得转载:便宜VPS网 » WordPress实用代码(WordPress将选中内容分享到新浪腾讯微博)