本文實例講述了jQuery實現(xiàn)鼠標選文字發(fā)新浪微博的方法。分享給大家供大家參考,具體如下:
最近注意到新浪博客有個小功能,就是當鼠標選中一段文字時會浮現(xiàn)一個小圖片,點擊這個圖片可以把選中內(nèi)容發(fā)送到新浪微博,一時興起昨晚就寫了一個Demo玩了一下,代碼超簡單,沒優(yōu)化,有興趣的朋友可以自己改進。
原理很簡單,先獲得鼠標選中文字,然后調(diào)用新浪博客中提供的頁面,把文字作為參數(shù)傳過去就OK了。
代碼如下:
<html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title></title> <style type="text/css"> .tooltip { width:120px; height:23px; line-height:23px; background-color:#CCCCCC; } .tooltip a { color: #333333; display: block; font-size: 12px; font-weight: bold; text-indent: 10px; } </style> <script src="jquery.min.js"></script> <script type="text/javascript"> $(function () { $("#blogContent").mouseup(function (e) { var x = 10; var y = 10; var r = ""; if (document.selection) { r = document.selection.createRange().text; } else if (window.getSelection()) { r = window.getSelection(); } if (r!= "") { var bowen = "發(fā)送到新浪微博"; var tooltip = "<div id='tooltip' class='tooltip'><a onclick=ask('"+r+"')>" + bowen + "</a></div>"; $("body").append(tooltip); $("#tooltip").css({ "top": (e.pageY + y) + "px", "left": (e.pageX + x) + "px", "position": "absolute" }).show("fast"); } }).mousedown(function () { $("#tooltip").remove(); }); }) function ask(r) { if (r != "") { window.open('http://v.t.sina.com.cn/share/share.php?searchPic=false&title='+r+'&url=http://www.nowwamagic.net&sourceUrl=http%3A%2F%2Fblog.sina.com.cn&content=utf-8&appkey=1617465124', '_blank', 'height=515, width=598, toolbar=no, menubar=no, scrollbars=auto, resizable=yes, location=no, status=yes'); } } </script></head><body> <div id="blogContent"> words words words words words words words words words。 </div></body></html> 就這么簡單哦,大家可以自己試試哈。當然獲得選中文本還可以有其他操作,這兒只是取巧調(diào)用了新浪的頁面,大家如果有興趣可以自己創(chuàng)建應用自己實現(xiàn)。



















