asp下实现代码的“运行代码”“复制代码”“保存代码”功能源码

本文介绍了一种在网页上实现代码片段高亮显示及直接运行的方法。通过简单的标记语法,用户可在文章中插入可执行代码块,并提供运行、复制及保存等功能。此功能适用于各种编程示例,便于读者即时测试代码效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Function content_Code(Str)
dim ary_String,i,n,n_pos
ary_String=split(Str,"[ code ]")
n=ubound(ary_String)
If n<1 then
content_Code=Str
Exit function
End If
for i=1 to n
n_pos=inStr(ary_String(i),"[/ code ]")
If n_pos>0 then
ary_String(i)="<br /><textarea class=gocode id=runcode"&i&" name=runcode"&i&">" & server.HTMLEncode(left(ary_String(i),n_pos-1))&"</textarea><br /><input type=button class=input-code value=运行代码 onclick=runCode(runcode"&i&")><input type=button class=input-code value=复制代码 onclick=copycode(runcode"&i&")><input type=button class=input-code value=另存代码 onclick=saveCode(runcode"&i&")><span class=code-tishi>提示:您可以先修改部分代码再运行</span><br />" & _
right(ary_String(i),len(ary_String(i))-n_pos-6)
Else
ary_String(i)="[code]" & ary_String(i)
End if
next
content_Code=join(ary_String,"")
End Function

使用方法也很简单:
发表文章时需用使用“运行代码”功能的,主要前后加上
[ code ][/ code ]
比如:
[复制此代码]CODE:
[ code ]
<script type="text/javascript">
alert("简单吧");
</script>
[/ code ]
需要注意的是,在显示的页面要进入以下javascript:

function runCode(obj) {
var winname = window.open('', "_blank", '');
winname.document.open('text/html', 'replace');
winname.opener = null
winname.document.writeln(obj.value);
winname.document.close();
}
function copycode(obj) {
var rng = document.body.createTextRange();
rng.moveToElementText(obj);
rng.scrollIntoView();
rng.select();
rng.execCommand("Copy");
rng.collapse(false);
}
function saveCode(obj) {
var winname = window.open('', '_blank', 'top=10000');
winname.document.open('text/html', 'replace');
winname.document.write(obj.value);
winname.document.execCommand('saveas','','code.htm');
winname.close();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值