本是不起眼的小东西,一旦需要时,到处淘不说,并且还不是那么好用,要改动点儿什么就会发现些怪问题出来。这里本意是想点击链接直接复制对应网址,也就是个 Ctrl+C 的功能:
<script>
function docsurl_getbyid(id) {
itm = null;
if (document.getElementById) {
itm = document.getElementById(id);
} else if (document.all) {
itm = document.all[id];
} else if (document.layers) {
itm = document.layers[id];
}
return itm;
}
function copydocurl(obj){
var rng = document.body.createTextRange();
rng.moveToElementText(obj);
rng.scrollIntoView();
rng.select();
rng.execCommand("Copy");
rng.collapse(false);
alert('该地址已经复制到剪贴板');
}
</script>
<a href="###" onclick="copydocurl(docsurl_getbyid('docurla'))"><span id="docurla" style="font-size: 11px;">http://www.goupwing.com</span></a>
function docsurl_getbyid(id) {
itm = null;
if (document.getElementById) {
itm = document.getElementById(id);
} else if (document.all) {
itm = document.all[id];
} else if (document.layers) {
itm = document.layers[id];
}
return itm;
}
function copydocurl(obj){
var rng = document.body.createTextRange();
rng.moveToElementText(obj);
rng.scrollIntoView();
rng.select();
rng.execCommand("Copy");
rng.collapse(false);
alert('该地址已经复制到剪贴板');
}
</script>
<a href="###" onclick="copydocurl(docsurl_getbyid('docurla'))"><span id="docurla" style="font-size: 11px;">http://www.goupwing.com</span></a>
因为实际需要,想显示不同的点击内容或者隐藏掉网址,用了几种方法都不好。在网页布局不是很标准的情况下。直接用“display:none; ”来隐藏 根本行不通,因为整个隐掉之后,对应 ID 也不再存在也就法复制。加个 DIV 用绝对定位层到不可见的座标,可是可以了,却在点击时有些异常。如果页面中有用表格的话,网页会收缩或是跳跃一定福度,看起来总是怪怪的。最后,还是不得不采用老方法,用隐藏文本搞定。
不用不知道,小问题也有大学问,呵呵!
PS:第一种方法并不局限于文字内容,如果提交内容可以支持 HTML 的话,你大可以定义内容为任何 HTML 元素。
<script>
function docsurl_getbyid(id) {
itm = null;
if (document.getElementById) {
itm = document.getElementById(id);
} else if (document.all) {
itm = document.all[id];
} else if (document.layers) {
itm = document.layers[id];
}
return itm;
}
function copydocurl(obj){
obj.select();
js=obj.createTextRange();
js.execCommand("Copy")
alert('该地址已经复制到剪贴板');
}
</script>
<a href="###" onclick="copydocurl(docsurl_getbyid('docurlb'))">白冰的追风岁月</a>
<input name="docsurl" id="docurlb" type="hidden" value="http://ichov.com"/>
function docsurl_getbyid(id) {
itm = null;
if (document.getElementById) {
itm = document.getElementById(id);
} else if (document.all) {
itm = document.all[id];
} else if (document.layers) {
itm = document.layers[id];
}
return itm;
}
function copydocurl(obj){
obj.select();
js=obj.createTextRange();
js.execCommand("Copy")
alert('该地址已经复制到剪贴板');
}
</script>
<a href="###" onclick="copydocurl(docsurl_getbyid('docurlb'))">白冰的追风岁月</a>
<input name="docsurl" id="docurlb" type="hidden" value="http://ichov.com"/>
Random Posts
暂无评论 Trackback Url:http://ichov.com/web/ctrlc.html/trackback


