Home  >  Article  >  Web Front-end  >  How to use js to copy part of the text in the link_javascript skills

How to use js to copy part of the text in the link_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:48:121195browse

The links on the web page generally look like a finger when you put the mouse on them, so you cannot drag the mouse to copy. The following JS allows you to copy. Save this code as a chrome bookmark, which needs to be copied. When you click on this bookmark and then hold down the ctrl key, you can copy the text above the link

The implementation code for copying part of the text in the link is as follows:

javascript: (function() {
  var h, checked = true,
  down = false;
  document.addEventListener('mouseover',
  function(e) {
    var link, c = '',
    target = e.target;
    if (target.nodeName == 'A') {
      if (target.hasChildNodes) {
        for (var i = 0; i < target.childNodes.length; i++) {
          if (target.childNodes[i].nodeName == 'INPUT') return;
        }
      }
      link = target;
    }
    if (target.parentNode.nodeName == 'A' && target.nodeName != 'IMG' && target.nodeName != 'INPUT') {
      link = target.parentNode;
    }
    if (!link) return;
    if (checked) {
      h = link.href;
      if (link.style.cssText) c = link.style.cssText;
    }
    function _click(e) {
      link.removeEventListener(e.type, arguments.callee, false);
      e.preventDefault();
    }
    function _keydown(e) {
      var k = parseInt(e.keyCode);
      if (k < 48 && k != 17) return;
      document.removeEventListener(e.type, arguments.callee, false);
      down = true;
      link.removeAttribute('href');
      link.setAttribute('style', c + 'cursor:text!important;');
      link.addEventListener('click', _click, false);
    }
    document.addEventListener('keydown', _keydown, false);
    link.addEventListener('mouseout',
    function(e) {
      var k = link.compareDocumentPosition(e.relatedTarget);
      if (k == 20 || k == 0) {
        checked = false;
      } else {
        link.removeEventListener(e.type, arguments.callee, false);
        link.removeEventListener('click', _click, false);
        document.removeEventListener('keydown', _keydown, false);
        checked = true;
        if (down) {
          down = false;
          link.setAttribute('href', h);
          if (c == '') {
            link.removeAttribute('style');
          } else {
            link.setAttribute('style', c);
          }
        }
      }
    },
    false);
  },
  false);
})();

The above is the implementation code for copying part of the text in the link. I hope you like it.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn