這篇文章介紹了使用js復(fù)制鏈接中的部分文字的方法,技巧很實(shí)用,需要的朋友可以參考下
網(wǎng)頁上面的鏈接一般鼠標(biāo)放上去就是一個手指的形狀,導(dǎo)致不能拖動鼠標(biāo)進(jìn)行復(fù)制,下面這段JS就是讓你能夠?qū)崿F(xiàn)復(fù)制的,將這段代碼保存成chrome的書簽,需要復(fù)制的時候點(diǎn)擊這個書簽,然后按著ctrl鍵,就可以復(fù)制鏈接上面的文字了
復(fù)制鏈接中的部分文字的實(shí)現(xiàn)代碼如下:
- 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);
- })();
以上就是復(fù)制鏈接中的部分文字的實(shí)現(xiàn)代碼,希望大家可以喜歡。
新聞熱點(diǎn)
疑難解答
圖片精選