让innerHTML方法添加到元素里的js可以被解析执行

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div id="test" style="background:skyblue">test innerHTMLJS</div>
<div onclick="say();" style="background:skyblue; margin:20px;">call say();</div>
<script type="text/javascript">
    function $id(id){return document.getElementById(id);}

    s = document.createElement('script');
    s.type="text/javascript";
    document.body.appendChild(s);

    $id('test').onclick=function(){
        var el = this,
        HTMLString='<img src="*" style="visibility:hidden;" onerror="s.innerHTML=$id(\'ss\').innerHTML;" \/><script id="ss">function say(){alert("hi i am executed");} say();<\/script>';
        el.innerHTML = HTMLString; return;
    };
</script>
</body>
</html>

 

 

posted @ 2014-01-09 14:03  stephenykk  阅读(1421)  评论(0编辑  收藏  举报