动态的往html中写js代码后,怎么执行新添加的代码,始终无效呢? 20

动态的往html中写js代码后,怎么执行新添加的代码,始终无效呢?<html><head><metahttp-equiv="Content-Type"content="t... 动态的往html中写js代码后,怎么执行新添加的代码,始终无效呢?

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>新建网页 3</title>
<script>
function include_js()
{
var sobj = document.createElement('script');
sobj.type = "text/javascript";
sobj.src = code.value;
var headobj = document.getElementsByTagName('head')[0];
headobj.appendChild(sobj);
}
</script>
</head>

<body>

<textarea rows="6" cols="21" id="code">function ceshi(){
alert("ok");
}</textarea><input type="button" value="写JS代码" name="B1"><input onclick="ceshi();" type="button" value="测试代码" name="B2"></p>

</body>

</html>
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>新建网页 3</title>
<script>
function add_js()
{
var sobj = document.createElement('script');
sobj.type = "text/javascript";
sobj.src = code.value;
var headobj = document.getElementsByTagName('head')[0];
headobj.appendChild(sobj);
}
</script>
</head>

<body>

<textarea rows="6" cols="21" id="code">function ceshi(){
alert("ok");
}</textarea><input type="button" value="写JS代码" onclick="add_js();" name="B1"><input onclick="ceshi();" type="button" value="测试代码" name="B2"></p>

</body>

</html>
展开
 我来答
ynyhuh
2007-03-29 · TA获得超过910个赞
知道小有建树答主
回答量:1595
采纳率:0%
帮助的人:1069万
展开全部
一、function include_js() 没有地方开启它。
二、onclick="ceshi();" 没有地方执行它。
三、<textarea rows="6" cols="21" id="code">function ceshi(){
alert("ok");
}</textarea> 作为<textarea>是文本区,function ceshi()就作为文本了,不算是JS代码。
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
帐号已注销
2007-04-11 · TA获得超过160个赞
知道答主
回答量:31
采纳率:0%
帮助的人:0
展开全部
为什么不用 eval 函数呢?
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>新建网页</title>
<script>
function execute(){
eval(code.value);
}
</script>
</head>
<body>
<textarea rows="6" cols="21" id="code">//js代码
alert("ok");
</textarea>
<input onclick="execute()" type="button" value="测试代码" name="B">
</body>
</html>
当然你可能是想得到函数的动态加载,而不单只是执行几条命令……这个我还没试
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
joythor
2007-03-30
知道答主
回答量:1
采纳率:0%
帮助的人:0
展开全部
<html>

<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">
<title>新建网页 3</title>
<script>
function include_js()
{
var sobj = document.createElement(\'script\');
sobj.type = \"text/javascript\";
//var src = document.createTextNode(document.getElementById(\'code\').value)
src = document.getElementById(\'code\').value;
eval(src);
//sobj.appendChild(src);
var headobj = document.getElementsByTagName(\'head\')[0];
headobj.appendChild(sobj);
}
</script>
</head>

<body>

<textarea rows=\"6\" cols=\"21\" id=\"code\">
</textarea><input type=\"button\" value=\"写JS代码\" name=\"B1\"><input onclick=\"include_js();\" type=\"button\" value=\"测试代码\" name=\"B2\"></p>

</body>

</html>
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式