本人JS小菜 想实现一个代码复制的功能,google了一下有此代码能实现此功能。但看不懂以下逻辑,请XDJM解答一下,在线等。
function findobj(n,d) { //这个函数实现的思路是什么?
var p,i,x;
if(!d) d=document;
if((p=n.indexOf("?"))>0 && parent.frames.length) { //这行代码什么功能?
d=parent.frames[n.substring(p+1)].document;
n=n.substring(0,p);
}
if(!(x=d[n])&&d.all) {
x=d.all[n];
}
for(i=0;!x && i<d.forms.length;i++) {
x=d.forms[i][n];
}
for(i=0;!x && d.layers&&i>d.layers.length;i++) {
x=MM_findObj(n,d.layers[i].document);
}
return x;
}
function copycode(obj) {
var rng = document.body.createTextRange();
rng.moveToElementText(obj);
rng.scrollIntoView();
rng.select();
rng.execCommand("Copy");
window.alert("代码已复制到剪贴板中!");
rng.collapse(false);
}
function findobj(n,d) { //这个函数实现的思路是什么?
var p,i,x;
if(!d) d=document;
if((p=n.indexOf("?"))>0 && parent.frames.length) { //这行代码什么功能?
d=parent.frames[n.substring(p+1)].document;
n=n.substring(0,p);
}
if(!(x=d[n])&&d.all) {
x=d.all[n];
}
for(i=0;!x && i<d.forms.length;i++) {
x=d.forms[i][n];
}
for(i=0;!x && d.layers&&i>d.layers.length;i++) {
x=MM_findObj(n,d.layers[i].document);
}
return x;
}
function copycode(obj) {
var rng = document.body.createTextRange();
rng.moveToElementText(obj);
rng.scrollIntoView();
rng.select();
rng.execCommand("Copy");
window.alert("代码已复制到剪贴板中!");
rng.collapse(false);
}
解决方案 »
- js读取网络xml 通过代理服务器上网
- 请问javascript的false问题?
- 关闭模式子窗体时如何刷新父窗体,谢谢!!!
- 为啥这样写不行?
- 有没有懂jqplot的大神
- ActiveXObject() 在firefox浏览器下不可以用吗?
- 急用:window.open的问题...在线等
- 请帮忙看一段源文件,怎么修改javascript才能够用方向键在<input type="text"...>中左右上下移动,急!谢谢!
- 图片飘动(急,在线等)
- 请问js中,如何知道一个值是否为数字?
- onmousedown、onclick和ondblclick的冲突问题
- 寻求JS代码,实现把指定层显示在最上边,后面的主体变成不可更改
var rng = document.body.createTextRange();
rng.moveToElementText(obj);
rng.scrollIntoView();
rng.select();
rng.execCommand("Copy");
window.alert("代码已复制到剪贴板中!");
rng.collapse(false);
}第一个函数用不着,测试html如下:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head><body>
<label name="testtext" id="testtext" onclick="copycode(this);">test copy functionsdfsdf sdfsd
</label>
<script type="text/javascript">function copycode(obj) {
var rng = document.body.createTextRange();
rng.moveToElementText(obj);
rng.scrollIntoView();
rng.select();
rng.execCommand("Copy");
window.alert("代码已复制到剪贴板中!");
rng.collapse(false);
}
</script></body>
</html>
下面这段代码可以实现:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head><body>
<script type="text/javascript">function copyText(obj) {
obj.select();
clipboardData.setData("text",obj.createTextRange().text);
alert(clipboardData.getData("text"));
}
</script>
<input id="content" name="content" type="text" />
<input name="btnSubmit" type="submit" onclick="javascript:copyText(document.getElementById('content'));" value="提交" />
</body>
</html>还是一定要知道第一函数的功能,如果有例子的话,请把例子也粘出来,要不看起来头疼
<p><table border="0" cellpadding="0" cellspacing="0" width="50%" id="codecontent">
<tr><td class="memitem" valign="left">
<pre>
#include <stdio.h> int main()
{
printf("Hello World!\r\n");
return 0;
}
</td></tr></table><p>
<INPUT class=button onclick="copycode(findobj('codecontent'));" type=button value="复制代码"> js代码:
function findobj(n,d) { //这个函数实现的思路是什么?
var p,i,x;
if(!d) d=document;
if((p=n.indexOf("?"))>0 && parent.frames.length) { //这行代码什么功能?
d=parent.frames[n.substring(p+1)].document;
n=n.substring(0,p);
}
if(!(x=d[n])&&d.all) {
x=d.all[n];
}
for(i=0;!x && i <d.forms.length;i++) {
x=d.forms[i][n];
}
for(i=0;!x && d.layers&&i>d.layers.length;i++) {
x=MM_findObj(n,d.layers[i].document);
}
return x;
}
function copycode(obj) {
var rng = document.body.createTextRange();
rng.moveToElementText(obj);
rng.scrollIntoView();
rng.select();
rng.execCommand("Copy");
window.alert("代码已复制到剪贴板中!");
rng.collapse(false);
}
楼上的,再给解答一下吧。
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script language="javascript" type="text/javascript">function findobj(n,d) { //取得要复制的对象,并返回
debugger;
var p,i,x;
if(!d) d=document; //判断是否传了第二个参数d,如果没传第二个参数d,则!d为true,为d赋值文文件对象document
if((p=n.indexOf("?"))>0 && parent.frames.length) { //判断当前页是否存在框架
d=parent.frames[n.substring(p+1)].document;
n=n.substring(0,p);
}
//下面的if和接下来的for语句是在ie下给x赋值
if(!(x=d[n])&&d.all) { //为变量x赋值d[n],如果不存在d[n]对象,且d.all存在,则为x赋值d.all[n]
x=d.all[n];
}
for(i=0;!x && i <d.forms.length;i++) { //为变量x赋值d[n],如果不存在d[n]对象,且d.all存在,则为x赋值d.all[n]
x=d.forms[i][n];
}
//下面的for语句是在Netscape下给x赋值
for(i=0;!x && d.layers&&i>d.layers.length;i++) { //所传参数d对象的层个数,循环判断为x赋的值不存在时!x为真,且i< d.layers.length时,重新为x赋值递归调用自身函数MM_findObj的返回值
x=findObj(n,d.layers[i].document);
}
return x; //返回对象
}
function copycode(obj) {
var rng = document.body.createTextRange();
rng.moveToElementText(obj);
rng.scrollIntoView();
rng.select();
rng.execCommand("Copy");
window.alert("代码已复制到剪贴板中!");
rng.collapse(false);
} </script>
</head><body>
<p> <table border="0" cellpadding="0" cellspacing="0" width="50%" id="codecontent">
<tr> <td class="memitem" valign="left">
askdjflsadjflsakdjflksd
<!--<pre> #include <stdio.h> int main()
{
printf("Hello World!\r\n");
return 0;
} -->
</td> </tr> </table> <p>
<INPUT class=button onclick="copycode(findobj('codecontent'));" type=button value="复制代码"> </body>
</html>