写了一个简单的js,但是存在错误:
<html>
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<title> 新建网页 1 </title>
<script language= "javascript ">
function init()
{
var str = " <option> aaaaaaaa </option> <option> adfadf </option> ";
document.getElementById( "sel ").innerHTML = str;
alert(document.getElementById( "sel ").outerHTML);
}
</script>
</head>
<body onload= "init() ">
<select id= "sel " name= "sel " SelectedIndex = "0 " >
<option value= "0 "> 请选择 </option>
</select>
</body>
</html>
上面是我写的一段html代码,
alert(document.getElementById( "sel ").outerHTML);
执行的结果是:
<select id= "sel " name= "sel " SelectedIndex = "0 " > aaaaaaaa </option> <option> adfadf </option> </select>
这是为什么?是因为字符串里含有转义字符吗?
------解决方案--------------------
//修改如下
<html>
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<title> 新建网页 1 </title>
<script language= "javascript ">
function init()
{
var str = " <select name=1 SelectedIndex = 0 > <option> aaaaaaaa </option> <option> adfadf </option> </select> ";
document.getElementById( "sel ").innerHTML = str;
alert(document.getElementById( "sel ").outerHTML);
}
</script>
</head>
<body onload= "init() ">
<table>
<tr> <td id= "sel "> </td> </tr> </table>
</body>
</html>
//alert的输出是正确的,它只输出字符串,对内容是不做解析的。