select.html 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title></title>
  6. <script type="text/javascript" src="http://cdn.static.runoob.com/libs/jquery/1.10.2/jquery.min.js"></script>
  7. <style type="text/css">
  8. .tooltip {
  9. width: 219px;
  10. height: 33px;
  11. }
  12. .tooltip a {
  13. width: 219px;
  14. height: 33px;
  15. display: block;
  16. }
  17. </style>
  18. <script type="text/javascript">
  19. // 获取选中的文本
  20. function getSelectText() {
  21. var txt = "";
  22. if (document.selection) {
  23. txt = document.selection.createRange().text;
  24. } else {
  25. txt = document.getSelection();
  26. //txt = window.getSelection();
  27. }
  28. return $.trim(txt.toString());
  29. }
  30. function getPosition(e) {
  31. var x = 15;
  32. var y = 15;
  33. var r = getSelectText();
  34. if (r) {
  35. var bowen = r.toString();
  36. var tooltip =
  37. "<div id='tooltip' class='tooltip'><a href='javascript:void(0)' target='_blank'>" +
  38. bowen +
  39. "</a></div>";
  40. $("body").append(tooltip);
  41. $("#tooltip")
  42. .css({
  43. top: e.pageY + y + "px",
  44. left: e.pageX + x + "px",
  45. position: "absolute",
  46. })
  47. .show("fast");
  48. }
  49. }
  50. $(function () {
  51. //将该id下的文章,鼠标选中松开后弹窗
  52. $("#selectedArticle")
  53. .mouseup(function (e) {
  54. var txt = getSelectText();
  55. txt = $.trim(txt);
  56. getPosition(e);
  57. })
  58. .mousedown(function () {
  59. $("#tooltip").remove();
  60. });
  61. });
  62. </script>
  63. </head>
  64. <body>
  65. <!--在文章内添加 selected-article ID-->
  66. <div>
  67. <textarea cols="60" rows="8" id="selectedArticle">
  68. asp、php、asp.net、javascript、jquery、vbscript、dos
  69. </textarea
  70. >
  71. </div>
  72. asp、php、asp.net、javascript、jquery、vbscript、dos
  73. </body>
  74. </html>