日期:2014-05-16  浏览次数:20518 次

在innerHTML中javascript不能执行,非常奇怪的问题!
<script>

function   flush()   {

var   obj =   document.getElementById( 'main-content ');
obj.innerHTML =   "aaa <script   defer> alert( 'ssff '); <\/script> ";
}
</script>

<a   href= "# "   onClick= "return   flush() "> ##### </a>

<div   id= "main-content "   class= "block ">
<form   name= "form2 "   action= "site_gd.php "   method= "post ">
<input   type= "text "   name= "sf "   />
</form>
</div>

问题非常奇怪
直接点链接不能执行js的alert
点一下input框再点链接就可以alert了
希望高人帮忙指点一下

------解决方案--------------------
在innerHTML中javascript本来就无法执行
他是按字符串来处理的
------解决方案--------------------
好像是非常奇怪
------解决方案--------------------
<script>

function flush() {

var obj = document.getElementById( 'main-content ');
obj.innerHTML = " "
obj.innerHTML = "aaa <script defer> alert( 'ssff '); <\/script> ";
}
</script>

<a href= "# " onClick= "return flush() "> ##### </a>

<div id= "main-content " class= "block ">
<form name= "form2 " action= "site_gd.php " method= "post ">
<input type= "text " name= "sf " />
</form>
</div>
------解决方案--------------------
发现一个问题

<input type= "text " name= "sf " />

在执行期间其代码为
<input name= "sf " >

然后造成不能alert 了...
大家研究研究...