zoukankan      html  css  js  c++  java
  • javascript中write( ) 和 writeln( )的区别

    writeln() 方法与 write() 方法作用相同,外加可在每个表达式后写一个换行符。

     

    1、Microsoft JScript 提供了两种方式来在浏览器中直接显示数据。可以使用 write( ) 和 writeln( ),这两个函数是document 对象的方法。

    2、writeln( ) 方法与 write( ) 方法几乎一样,差别仅在于是前者将在所提供的任何字符串后添加一个换行符。在 HTML 中,这通常只会在后面产生一个空格;不过如果使用了 <PRE> 和 <XMP> 标识,这个换行符会被解释,且在浏览器中显示。遇到</PRE>或者writeln()都会换行.

    3、在调用 write( ) 方法时,如果该文档不处于在调用 write( ) 方法时的打开和分析的过程中,该方法将打开并清除该文档,所以它可能是有危险的。

     

    示例:  

     1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     2 <html xmlns="http://www.w3.org/1999/xhtml">
     3 <head>
     4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     5 <title>model</title>
     6 <script>
     7 function DocWrite()
     8 {
     9 //<PRE>体现出"2"的问题
    10 document.write("<h1>这是使用document.write输出的</h1>");
    11 document.writeln("<h1>这是使用<PRE>document.writeln输出的</PRE></h1>");
    12 }
    13 </script>
    14 </head>
    15 <body>
    16 <!--两处调用DocWrite()体现出"3"的问题  按下按钮 则  "正文内容" 消失-->
    17 <script>
    18 DocWrite();
    19 </script>
    20 <p>正文内容</p>
    21 </p><input type="button" name="Submit" value="按钮" onclick="DocWrite()"></p>
    22 </body>
    23 </html>
     1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     2 <html xmlns="http://www.w3.org/1999/xhtml">
     3 <head>
     4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     5 <title>model</title>
     6 <script language="javascript">
     7 document.write("使用writeln方法输出的数据会进行换行<pre>");
     8 for(x=1;x<=3;x++){
     9     document.writeln(x+"朵花");  //document.write("123456789");
    10     document.writeln("<img src='img/d1.gif'>");
    11 }
    12 document.write("</pre>")
    13 </script>
    14 </head>
    15 <body>
    16 使用write方法输出的数据会连在一起不分开<br>
    17 <script language="javascript">
    18 for(x=1;x<=3;x++){
    19     document.write(x+"朵花");
    20     document.write("<img src='img/d2.gif'>");
    21 }
    22 </script>
    23 </body>
    24 </html>
  • 相关阅读:
    世界充满神秘的不平衡:创业中的“二八”法则
    树立个人品牌:从名字开始
    房子,心中的痛
    今生,谁会是我最美丽的新娘
    失败不是创业的结束,因为有了你们,这世界才璀璨(转载)
    再等五百年
    名字作诗:为您的名字增添一份色彩
    揭开爱情的外衣
    李子楠(帮客户名字作诗)
    爱上你,早已是命中注定
  • 原文地址:https://www.cnblogs.com/vincent_ds/p/2584243.html
Copyright © 2011-2022 走看看