分享一段选中文字分享到微博的代码[转]

两天 2011年11月1日22:45:00资源分享1318,7731字数 2345阅读7分49秒阅读模式

分享一段选中文字分享到微博的代码[转]

代码:

  1. <STYLE>  
  2. .img_sina_share {  
  3. DISPLAY: none; CURSOR: pointer; POSITION: absolute  
  4. }  
  5. .img_qq_share {  
  6. DISPLAY: none; CURSOR: pointer; POSITION: absolute  
  7. }  
  8. </STYLE>  
  9. <IMG   
  10. class=img_sina_share id=imgSinaShare title=将选中内容分享到新浪微博   
  11. src="http://simg.sinajs.cn/blog7style/images/common/share.gif">  
  12. <IMG   
  13. class=img_qq_share id=imgQqShare title=将选中内容分享到腾讯微博   
  14. src="http://open.t.qq.com/images/resource/weiboicon32.png" weight="26" height="26">  
  15. <SCRIPT>  
  16. var eleImgShare = document.getElementById("imgSinaShare"); //新浪微博图标  
  17. var eleImgShare2 = document.getElementById("imgQqShare"); //腾讯微博图标  
  18.   
  19. var $miniBlogShare = function(eleShare,eleShare2,eleContainer) { //实现方法  
  20. var eleTitle = document.getElementsByTagName("title")[0];  
  21. eleContainer = eleContainer || document;  
  22. var funGetSelectTxt = function() { //获取选中文字  
  23. var txt = "";  
  24. if(document.selection) {  
  25. txt = document.selection.createRange().text; // IE  
  26. else {  
  27. txt = document.getSelection();  
  28. }  
  29. return txt.toString();  
  30. };  
  31. eleContainer.onmouseup = function(e) { //限定容器若有文字被选中  
  32. e = e || window.event;  
  33. var txt = funGetSelectTxt(), sh = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;  
  34. 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;  
  35. if (txt) {  
  36. eleShare.style.display = "inline";  
  37. eleShare.style.left = left + "px";  
  38. eleShare.style.top = top + "px";  
  39. eleShare2.style.display = "inline";  
  40. eleShare2.style.left = left + 30 + "px";  
  41. eleShare2.style.top = top + "px";  
  42. else {  
  43. eleShare.style.display = "none";  
  44. eleShare2.style.display = "none";  
  45. }  
  46. };  
  47. eleShare.onclick = function() { //点击新浪微博图标  
  48. var txt = funGetSelectTxt(), title = (eleTitle && eleTitle.innerHTML)? eleTitle.innerHTML : "未命名页面";  
  49. if (txt) {  
  50. window.open('http://v.t.sina.com.cn/share/share.php?title=' + txt + '→来自页面"' + title + '"的文字片段&url=' + window.location.href);   
  51. }  
  52. };  
  53. eleShare2.onclick = function() { //点击腾讯微博图标  
  54. var txt = funGetSelectTxt(), title = (eleTitle && eleTitle.innerHTML)? eleTitle.innerHTML : "未命名页面";  
  55. if (txt) {  
  56. window.open( 'http://v.t.qq.com/share/share.php?title=' + encodeURIComponent(txt + '→来自页面"' + title + '"的文字片段&url=' + window.location.href));   
  57. }  
  58. };  
  59. }(eleImgShare,eleImgShare2);  
  60. </SCRIPT>  

效果可以参考本微薄

来自:http://www.discuz.net/thread-2077854-1-1.html
部分修改。

相关文章
继续阅读
  • 我的QQ
  • 这是我的qq扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin
两天
  • 本文由 发表于 2011年11月1日22:45:00
  • 转载请务必保留本文链接:https://2days.org/2231.html
评论  13  访客  8  作者  5
    • 陈雨萌
      陈雨萌 9

      一起交流!对这个话题感兴趣的朋友们

      • 陈雨萌
        陈雨萌 9

        一起交流!对这个话题感兴趣的朋友们

        • 小范
          小范 1

          哈哈 终于好了
          一样的主题
          这个前几天看到了,还没试
          :grin:

            • 两天
              两天

              @ 小范 :grin: 哈哈,这个主题貌似还不错 :lol:

            • 银杏
              银杏 0

              路过,感觉你写的不错,所以留下自己的脚印

              • Junan
                Junan 5

                :grin: 围观一下 暂时没这样需求 在博客上面加微薄信息~

                • 皇家元林
                  皇家元林 1

                  嗯,在新浪,腾讯的网站里就有这样的效果

                  • 米多
                    米多 1

                    每次看到技术文章,米多都只能看留言,。。。。看不懂

                      • 两天
                        两天

                        @ 米多 也没什么深奥的,复制粘贴而已~~ :grin:

                      • 朵未
                        朵未 6

                        效果不错嘛~ :grin:

                          • 两天
                            两天

                            @ 朵未 呵呵,有时候也许能够用到的说~~~ :smile:

                        匿名

                        发表评论

                        匿名网友 填写信息

                        :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

                        确定