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

JavaScript另存代码的问题
HTML code

<html>
<head>
<style type="text/css">
<!--
   textarea{
       width:500px;
       height:150px;
       border:1px solid #000;
       border-right:1px solid #C2C2C2;
       border-bottom:1px solid #c2c2c2; 
   }   
-->
</style>
<script language="javascript">
<!--
   function save(){
        var tcode=document.getElementById('code').value;
       document.execCommand('SaveAs','','code.html');
   }
   -->
</script>
</head>
<body>
       <textarea id="code">
          
          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
         <title>HTML Template</title>
      <style text="text/css">
      <!--
            #layout{
                margin:0px auto;
                background-color:#ccc;
                border:2px solid #333;
                width:300px;
                height:300px;
            }
      -->
      </style>
  </head>
  <body>
        <div id="layout">1列固定宽度</div>
  </body>
</html>

       </textarea>
       <br>
       <button onclick="save()">另存代码</button>
</body>
</html>


问下怎么可以只保存textarea中的代码

------解决方案--------------------
<button onclick="document.execCommand('Saveas',false,'')">另存代码</button>
------解决方案--------------------
<html>
<head>
<style type="text/css">
<!--
textarea{
width:500px;
height:150px;
border:1px solid #000;
border-right:1px solid #C2C2C2;
border-bottom:1px solid #c2c2c2; 
}
-->
</style>
<script language="javascript">
<!--
function save(){
var tcode=document.getElementById('code').value;
hiddenFrame.document.write(tcode);
hiddenFrame.document.execCommand('SaveAs','false','code.html');
}
-->
</script>
</head>
<body>
<textarea id="code">

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>HTML Template</title>
<style text="text/css">
<!--
#layout{
margin:0px auto;
background-color:#ccc;
border:2px solid #333;
width:300px;
height:300px;
}
-->
</style>
</head>
<body>
<div id="layout">1列固定宽度</div>
</body>
</html>

</textarea>
<br>
<button onclick="save()">另存代码</button>
<IFRAME NAME="hiddenFrame" SRC="a.html" WIDTH="0" HEIGHT="0"></IFRAME>
</body>
</html>

a.html为任何一个页面/


------解决方案--------------------
学习。
------解决方案--------------------
等待牛人来答.