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

来源:百度知道 编辑:UC知道 时间:2024/05/31 09:48:13
动态的往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>

&l

一、function include_js() 没有地方开启它。
二、onclick="ceshi();" 没有地方执行它。
三、<textarea rows="6" cols="21" id="code">function ceshi(){
alert("ok");
}</textarea> 作为<textarea>是文本区,function ceshi()就作为文本了,不算是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\";
//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(