选择文字转发到微博的JS

网络整理的选择文字转发到微博的JS,以备后用。

直接引入,把需要实现选择转发的标签块改掉就可以实现效果。

 1 var sinaImg = '<img style="display: none; cursor: pointer; position: absolute; width:26px; height:26px;" id="imgSinaShare" title="将选中内容分享到新浪微博" src="http://simg.sinajs.cn/blog7style/images/common/share.gif"/>';
 2 var qqImg = '<img style="display: none; cursor: pointer; position: absolute; width:25px; height:25px;" id="imgQqShare" title="将选中内容分享到腾讯微博" src="http://open.t.qq.com/images/resource/weiboicon32.png">';
 3 document.write(sinaImg);
 4 document.write(qqImg);
 5 var sinaImgShare = document.getElementById("imgSinaShare");
 6 var qqImgShare = document.getElementById("imgQqShare");
 7 var artMain = document.getElementById("share");  //需要实现选择转发到微博的标签块
 8 var $miniBlogShare = function(sinaShare, qqShare, eleContainer) {
 9     var eleTitle = document.getElementsByTagName("title")[0];
10     eleContainer = eleContainer || document;
11     var funGetSelectTxt = function() {
12         var txt = "";
13         if (document.selection) {
14             txt = document.selection.createRange().text;
15         } else {
16             txt = document.getSelection();
17         }
18         return txt.toString();
19     };
20     eleContainer.onmouseup = function(e) {
21         e = e || window.event;
22         var txt = funGetSelectTxt(),
23         sh = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
24         var left = (e.clientX - 40 < 0) ? e.clientX + 20: e.clientX - 40,
25         top = (e.clientY - 40 < 0) ? e.clientY + sh + 20: e.clientY + sh - 40;
26         if (txt) {
27             sinaShare.style.display = "inline";
28             sinaShare.style.left = left + "px";
29             sinaShare.style.top = top + "px";
30             qqShare.style.display = "inline";
31             qqShare.style.left = left + 30 + "px";
32             qqShare.style.top = top + "px";
33         } else {
34             sinaShare.style.display = "none";
35             qqShare.style.display = "none";
36         }
37     };
38     sinaShare.onclick = function() {
39         var txt = funGetSelectTxt(),
40         title = (eleTitle && eleTitle.innerHTML) ? eleTitle.innerHTML: "未命名页面";
41         if (txt) {
42             window.open('http://v.t.sina.com.cn/share/share.php?ralateuid=2719305811&title=' + txt + '→来自页面"' + title + '"的文字片段&url=' + window.location.href);
43         }
44     };
45     qqShare.onclick = function() {
46         var txt = funGetSelectTxt(),
47         title = (eleTitle && eleTitle.innerHTML) ? eleTitle.innerHTML: "未命名页面";
48         if (txt) {
49             window.open('http://v.t.qq.com/share/share.php?title=' + encodeURIComponent(txt + '→来自页面"' + title + '"的文字片段&url=' + window.location.href));
50         }
51     };
52 } (sinaImgShare, qqImgShare, artMain);
posted @ 2012-07-19 11:50  Zhou Hr  阅读(714)  评论(0编辑  收藏  举报