Wordpress怎么将选中内容分享到新浪微博

 2020-01-07 10:10:48  ADMIN

Wordpress怎么将选中内容分享到新浪微博

wordpress怎么将选中内容分享到新浪微博?

本文实例讲述了Wordpress将选中内容分享到新浪腾讯微博的方法。分享给大家供大家参考。具体方法如下:

推荐:《wordpress教程

1、引入jQuery,相信大多数WordPress博客都已经引入了jQuery,那就可以直接进行第二步了.

2、在页面底部,或者更确切的说,在引入jQuery库的后面加上这样一段JS,你就可以看到和本站一样的效果了.

选中即分享的功能看上去比较高级,其实实现是相当简单的,其中的会让人头大,一般人也不感兴趣的原理这里就直接跳过,这个js文字选中后分享到新浪微博的功能我简单的封装了下,方法名是:$sinaMiniBlogShare

实例代码如下:

代码如下:

var miniBlogShare = function() { 
//指定位置驻入节点 
$('@@##@@@@##@@').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) { 
$(&#39;#imgSinaShare&#39;).css({ 
display : &#39;inline&#39;, 
left : left, 
top : top 
}); 
$(&#39;#imgQqShare&#39;).css({ 
display : &#39;inline&#39;, 
left : left + 30, 
top : top 
}); 
} else { 
$(&#39;#imgSinaShare&#39;).css(&#39;display&#39;, &#39;none&#39;); 
$(&#39;#imgQqShare&#39;).css(&#39;display&#39;, &#39;none&#39;); 
} 
}); 
//点击新浪微博 
$(&#39;#imgSinaShare&#39;).click(function() { 
var txt = funGetSelectTxt(), title = $(&#39;title&#39;).html(); 
if (txt) { 
window.open(&#39;http://v.t.sina.com.cn/share/share.php?title=&#39; + txt + &#39; —— 转载自:&#39; + title + &#39;&url=&#39; + window.location.href); 
} 
}); 
//点击腾讯微博 
$(&#39;#imgQqShare&#39;).click(function() { 
var txt = funGetSelectTxt(), title = $(&#39;title&#39;).html(); 
if (txt) { 
window.open(&#39;http://v.t.qq.com/share/share.php?title=&#39; + encodeURIComponent(txt + &#39; —— 转载自:&#39; + title) + &#39;&url=&#39; + window.location.href); 
} 
}); 
}();
登录后复制
x

在线咨询

微信客服

售后技术

QQ咨询