使用js復制鏈接中的部分文字的方法
更新時間:2015年07月30日 15:00:43 作者:彩虹糖tang
這篇文章介紹了使用js復制鏈接中的部分文字的方法,技巧很實用,需要的朋友可以參考下
網(wǎng)頁上面的鏈接一般鼠標放上去就是一個手指的形狀,導致不能拖動鼠標進行復制,下面這段JS就是讓你能夠實現(xiàn)復制的,將這段代碼保存成chrome的書簽,需要復制的時候點擊這個書簽,然后按著ctrl鍵,就可以復制鏈接上面的文字了
復制鏈接中的部分文字的實現(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);
})();
以上就是復制鏈接中的部分文字的實現(xiàn)代碼,希望大家可以喜歡。
相關文章
javascript Firefox與IE 替換節(jié)點的方法
Firefox 與 IE 替換節(jié)點的方法2010-02-02
Javascript 調用 ActionScript 的簡單方法
在Flex中,ActionScript調用Javascript是比較簡單的,說白了就是,在html里,怎么調用Javascript,在ActionScript就怎么調用就可以了。接下來通過本文給大家介紹js 調用 actionscript方法,感興趣的朋友一起看看吧2016-09-09
JavaScript創(chuàng)建類/對象的幾種方式概述及實例
JS中的對象強調的是一種復合類型,JS中創(chuàng)建對象及對對象的訪問是極其靈活的,下面與大家分享下創(chuàng)建類/對象的幾種方式,感興趣的朋友可以了解下哈2013-05-05
JS.GetAllChild(element,deep,condition)使用介紹
JS.GetAllChild()獲取所有子節(jié)點,想必大家都知道吧,具體的使用方法如下,感興趣的朋友可以參考下2013-09-09

