代码:文章源自两天的博客-https://2days.org/2231.html
<STYLE>
.img_sina_share {
DISPLAY: none; CURSOR: pointer; POSITION: absolute
}
.img_qq_share {
DISPLAY: none; CURSOR: pointer; POSITION: absolute
}
</STYLE>
<IMG
class=img_sina_share id=imgSinaShare title=将选中内容分享到新浪微博
src="http://simg.sinajs.cn/blog7style/images/common/share.gif">
<IMG
class=img_qq_share id=imgQqShare title=将选中内容分享到腾讯微博
src="http://open.t.qq.com/images/resource/weiboicon32.png" weight="26" height="26">
<SCRIPT>
var eleImgShare = document.getElementById("imgSinaShare"); //新浪微博图标
var eleImgShare2 = document.getElementById("imgQqShare"); //腾讯微博图标
var $miniBlogShare = function(eleShare,eleShare2,eleContainer) { //实现方法
var eleTitle = document.getElementsByTagName("title")[0];
eleContainer = eleContainer || document;
var funGetSelectTxt = function() { //获取选中文字
var txt = "";
if(document.selection) {
txt = document.selection.createRange().text; // IE
} else {
txt = document.getSelection();
}
return txt.toString();
};
eleContainer.onmouseup = function(e) { //限定容器若有文字被选中
e = e || window.event;
var txt = funGetSelectTxt(), sh = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
var 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) {
eleShare.style.display = "inline";
eleShare.style.left = left + "px";
eleShare.style.top = top + "px";
eleShare2.style.display = "inline";
eleShare2.style.left = left + 30 + "px";
eleShare2.style.top = top + "px";
} else {
eleShare.style.display = "none";
eleShare2.style.display = "none";
}
};
eleShare.onclick = function() { //点击新浪微博图标
var txt = funGetSelectTxt(), title = (eleTitle && eleTitle.innerHTML)? eleTitle.innerHTML : "未命名页面";
if (txt) {
window.open('http://v.t.sina.com.cn/share/share.php?title=' + txt + '→来自页面"' + title + '"的文字片段&url=' + window.location.href);
}
};
eleShare2.onclick = function() { //点击腾讯微博图标
var txt = funGetSelectTxt(), title = (eleTitle && eleTitle.innerHTML)? eleTitle.innerHTML : "未命名页面";
if (txt) {
window.open( 'http://v.t.qq.com/share/share.php?title=' + encodeURIComponent(txt + '→来自页面"' + title + '"的文字片段&url=' + window.location.href));
}
};
}(eleImgShare,eleImgShare2);
</SCRIPT>
效果可以参考本微薄文章源自两天的博客-https://2days.org/2231.html
来自:http://www.discuz.net/thread-2077854-1-1.html 本站文章大部分始于原创,用于个人学习记录,可能对您有所帮助,仅供参考!
部分修改。文章源自两天的博客-https://2days.org/2231.html
文章源自两天的博客-https://2days.org/2231.html
312379857
微信号已复制←QQ扫一扫添加QQ好友
版权声明:本站原创文章转载请注明文章出处及链接,谢谢合作!
- 代码
- 新浪微博
- 微博

![分享一段选中文字分享到微博的代码[转]](https://2days.org/wp-content/uploads/2011/11/weibo-share.png)
![分享一段选中文字分享到微博的代码[转]](https://2days.org/static/images/qr/pay-alipay-240.png)


8F
一起交流!对这个话题感兴趣的朋友们
7F
一起交流!对这个话题感兴趣的朋友们
6F
哈哈 终于好了

一样的主题
这个前几天看到了,还没试
B1
@ 小范
哈哈,这个主题貌似还不错 
5F
路过,感觉你写的不错,所以留下自己的脚印
4F
B1
@ Junan 就是随便玩玩,呵呵~~~
3F
嗯,在新浪,腾讯的网站里就有这样的效果
B1
@ 皇家元林 嗯,我就是看到才搜索了一下分享的~~~
2F
每次看到技术文章,米多都只能看留言,。。。。看不懂
B1
@ 米多 也没什么深奥的,复制粘贴而已~~
浙江 温州 1F
效果不错嘛~
B1
@ 朵未 呵呵,有时候也许能够用到的说~~~