逛CSDN找来的代码

 

    随便在CSDN外包频道上逛,找到一个这样的东东:

     Web 页面上的选取区域的记录和恢复

看了2分钟才算明白过来是什么意思,其实是选择当前页面的某些内容,将这些选择保存起来.

地址: http://home.arcor.de/martin.honnen/javascript/storingSelection1.html

代码先记下,在firefox中可用,IE需要修改

 

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  2.           "http://www.w3.org/TR/html4/strict.dtd">
  3. <html lang="en">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  6. <title>storing Mozilla selection as XPath, offset pair</title>
  7. <script type="text/javascript">
  8. function setCookie (cookieName, cookieValue, expires, path, domain, secure) {
  9.   document.cookie = 
  10.     escape(cookieName) + '=' + escape(cookieValue) 
  11.     + (expires ? '; EXPIRES=' + expires.toGMTString() : '')
  12.     + (path ? '; PATH=' + path : '')
  13.     + (domain ? '; DOMAIN=' + domain : '')
  14.     + (secure ? '; SECURE' : '');
  15. }
  16. function getCookie (cookieName) {
  17.   var cookieValue = null;
  18.   var posName = document.cookie.indexOf(escape(cookieName) + '=');
  19.   if (posName != -1) {
  20.     var posValue = posName + (escape(cookieName) + '=').length;
  21.     var endPos = document.cookie.indexOf(';', posValue);
  22.     if (endPos != -1)
  23.       cookieValue = unescape(document.cookie.substring(posValue, endPos));
  24.     else
  25.       cookieValue = unescape(document.cookie.substring(posValue));
  26.   }
  27.   return cookieValue;
  28. }
  29. </script>
  30. <script type="text/javascript">
  31. function makeXPath (node, currentPath) {
  32.   /* this should suffice in HTML documents for selectable nodes, XML with namespaces needs more code */
  33.   currentPathcurrentPath = currentPath || '';
  34.   switch (node.nodeType) {
  35.     case 3:
  36.     case 4:
  37.       return makeXPath(node.parentNode, 'text()[' + (document.evaluate('preceding-sibling::text()', node, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null).snapshotLength + 1) + ']');
  38.     case 1:
  39.       return makeXPath(node.parentNode, node.nodeName + '[' + (document.evaluate('preceding-sibling::' + node.nodeName, node, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null).snapshotLength + 1) + ']' + (currentPath ? '/' + currentPath : ''));
  40.     case 9:
  41.       return '/' + currentPath;
  42.     default:
  43.       return '';
  44.   }
  45. }
  46. function storeSelection () {
  47.   if (typeof window.getSelection != 'undefined') {
  48.     var selection = window.getSelection();
  49.     var range = selection.getRangeAt(0);
  50.     if (range != null) {
  51.       setCookie('sel', makeXPath(range.startContainer) + '|' + range.startOffset + '|' + makeXPath(range.endContainer) + '|' + range.endOffset);
  52.     }
  53.   }
  54. }
  55. function restoreSelection () {
  56.   var selectionDetails = getCookie('sel');
  57.   if (selectionDetails != null) {
  58.     selectionDetailsselectionDetails = selectionDetails.split(//|/g);
  59.     if (typeof window.getSelection != 'undefined') {
  60.       var selection = window.getSelection();
  61.       selection.removeAllRanges();
  62.       var range = document.createRange();
  63.       range.setStart(document.evaluate(selectionDetails[0], document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue, Number(selectionDetails[1]));
  64.       range.setEnd(document.evaluate(selectionDetails[2], document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue, Number(selectionDetails[3]));
  65.       selection.addRange(range);
  66.     }
  67.   }
  68. }
  69. window.onload = restoreSelection;
  70. </script>
  71. </head>
  72. <body>
  73. <h1>storing Mozilla selection as XPath, offset pair</h1>
  74. <div>
  75. <input type="button" value="mouse here to store current selection"
  76.        onmouseover="storeSelection();">
  77. </div>
  78. <p>Kibology for all. All for Kibology. <img src="kiboInside.gif" alt="Kibo inside"></p>
  79. </body>
  80. </html>
  81.       

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CSDN网站是一个提供技术资源、编程教程和社区交流的在线平台,而R语言是一种专门用于统计分析和数据可视化的编程语言。在CSDN网站上,我们可以到很多关于R语言的教程、文章和代码示例。 在CSDN网站上,我们可以通过搜索功能到关于R语言的相关资料。我们可以从入门教程开始学习R语言的基础知识,了解如何安装R语言环境、基本的语法和数据类型。在进一步学习之后,我们可以到更高级的R语言编程技巧、各种统计分析方法的实现以及数据可视化的方法。 除了教程之外,CSDN网站还提供了很多R语言的代码示例。这些代码示例可以帮助我们更好地理解R语言的使用方法,例如如何导入和处理数据、如何进行统计分析、如何绘制图表等等。通过运行这些示例代码,我们可以更快地掌握R语言的编程技巧和应用场景。 在CSDN的R语言社区,我们还可以和其他R语言爱好者进行交流和讨论。在这个社区,我们可以分享自己的代码和经验,向其他人提问和解答问题,共同进步。通过这个社区,我们可以结识更多对R语言感兴趣的人,相互学习和交流,拓宽自己的视野。 总之,CSDN网站是一个非常有价值的资源,可以帮助我们学习和使用R语言。从入门到掌握,我们可以通过CSDN网站到各种教程、代码示例和社区资源,提高自己的R语言编程能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值