首页 > 语言 > JavaScript > 正文

使用js复制链接中的部分文字的方法

2024-05-06 16:24:05
字体:大 中 小
来源:转载
供稿:网友

这篇文章介绍了使用js复制链接中的部分文字的方法,技巧很实用,需要的朋友可以参考下

网页上面的链接一般鼠标放上去就是一个手指的形状,导致不能拖动鼠标进行复制,下面这段JS就是让你能够实现复制的,将这段代码保存成chrome的书签,需要复制的时候点击这个书签,然后按着ctrl键,就可以复制链接上面的文字了

复制链接中的部分文字的实现代码如下:

 

 
  1. javascript: (function() { 
  2. var h, checked = true, 
  3. down = false; 
  4. document.addEventListener('mouseover', 
  5. function(e) { 
  6. var link, c = '', 
  7. target = e.target; 
  8. if (target.nodeName == 'A') { 
  9. if (target.hasChildNodes) { 
  10. for (var i = 0; i < target.childNodes.length; i++) { 
  11. if (target.childNodes[i].nodeName == 'INPUT') return; 
  12. } 
  13. } 
  14. link = target; 
  15. } 
  16. if (target.parentNode.nodeName == 'A' && target.nodeName != 'IMG' && target.nodeName != 'INPUT') { 
  17. link = target.parentNode; 
  18. } 
  19. if (!link) return; 
  20. if (checked) { 
  21. h = link.href; 
  22. if (link.style.cssText) c = link.style.cssText; 
  23. } 
  24. function _click(e) { 
  25. link.removeEventListener(e.type, arguments.callee, false); 
  26. e.preventDefault(); 
  27. } 
  28. function _keydown(e) { 
  29. var k = parseInt(e.keyCode); 
  30. if (k < 48 && k != 17) return; 
  31. document.removeEventListener(e.type, arguments.callee, false); 
  32. down = true; 
  33. link.removeAttribute('href'); 
  34. link.setAttribute('style', c + 'cursor:text!important;'); 
  35. link.addEventListener('click', _click, false); 
  36. } 
  37. document.addEventListener('keydown', _keydown, false); 
  38. link.addEventListener('mouseout', 
  39. function(e) { 
  40. var k = link.compareDocumentPosition(e.relatedTarget); 
  41. if (k == 20 || k == 0) { 
  42. checked = false; 
  43. } else { 
  44. link.removeEventListener(e.type, arguments.callee, false); 
  45. link.removeEventListener('click', _click, false); 
  46. document.removeEventListener('keydown', _keydown, false); 
  47. checked = true; 
  48. if (down) { 
  49. down = false; 
  50. link.setAttribute('href', h); 
  51. if (c == '') { 
  52. link.removeAttribute('style'); 
  53. } else { 
  54. link.setAttribute('style', c); 
  55. } 
  56. } 
  57. } 
  58. }, 
  59. false); 
  60. }, 
  61. false); 
  62. })(); 

以上就是复制链接中的部分文字的实现代码,希望大家可以喜欢。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选