1.
<html>
<head>
<title></title>
<script type="text/javascript">
function register(fn,text){
var head = document.getElementsByTagName("head")[0];
var script = document.createElement("script");
script.type = "text/javascript";
script.text = "function " + fn + "() {" + text + "}";
head.appendChild(script);
}
function say(){}
</script>
</head>
<body>
<input type="text" value="alert('We will rockyou')" onclick="register('say',this.value)"/>
<input type="button" onclick="say()" />
</body>
</html>
<html>
<head>
<title></title>
<script type="text/javascript">
function register(fn,text){
var head = document.getElementsByTagName("head")[0];
var script = document.createElement("script");
script.type = "text/javascript";
script.text = "function " + fn + "() {" + text + "}";
head.appendChild(script);
}
function say(){}
</script>
</head>
<body>
<input type="text" value="alert('We will rockyou')" onclick="register('say',this.value)"/>
<input type="button" onclick="say()" />
</body>
</html>
本文介绍了一种使用HTML和JavaScript动态注册函数的方法。通过在输入框中定义函数体,并在点击按钮时将该函数注册到页面中,实现了动态功能扩展。这种方法可用于教育、演示或快速原型开发场景。

被折叠的 条评论
为什么被折叠?



