可以的,需要把 </script>改为 <\/script>;例如:<html><head><script type="text/javascript">function createNewDoc(){var txts="";var newDoc=document.open("text/html","replace");var txt="<html><body>Learning about the DOM is FUN!\<input type='button' value='dynamic response'/> \<script> function warning(){ alert(\"new document\");}\document.getElementsByTagName('input')[0].addEventListener('click',warning);\<\/script><\/body><\/html>";console.log(txt);newDoc.write(txt);newDoc.close();}</script><body><input type="button" value="Write to a new document"onclick="createNewDoc()"></body></html>