• 【转】 JavaScript中With 语句使用方法实例


    内容导读

       有了 With 语句,在存取对象属性和方法时就不用重复指定参考对象,在 With 语句块中,凡是 JavaScript 不识别的属性和方法都和该语句块指定的对象有关。

      With 语句的语法格式如下所示:
          With Object {
            Statements
          }
          对象指明了当语句组中对象缺省时的参考对象,这里我们用较为熟悉的 Document 对象对 With 语句举例。例如 当使用与 Document 对象有关的 write( )或 writeln( )方法时,往往使用如下形式:
          document.writeln(”Hello!“)
          如果需要显示大量数据时,就会多次使用同样的 document.writeln()语句,这时就可以像下面的程序那样,把所有以 Document 对象为参考对象的语句放到With 语句块中,从而达到减少语句量的目的。下面是一个With 语句使用的例子:
          <html>
          <head>
            <title>JavaScript Unleashed</title>
          </head>
          <body>
            <script type="text/javascript">
            <!—
                with(document){
                  write("您好 !");
                  write("<br>这个文档的标题是 : \"" + title + "\".");
                  write("<br>这个文档的 URL 是: " + URL);
                  write("<br>现在您不用每次都写出 document 对象的前缀了 !");
               }
            // -->
            </script>
          </body>
          </html>
      这样,您在使用 document 的方法和属性时就可以去掉 Document 前缀。
      请注意程序中的 title 和 URL 均是 Document 对 象的属性,一般情况下应写作document.title 和document.URL 使用 With 语句,您只需指定一次参考对象,这同把每一行都用document.writeln()打印下来的结果一样,这个例子的执行结果如图1所示
      注意:由于浏览器的不同在本例中您可能看到,URL 的一种编码格式。

    转自SurpassLi 的博客

    原文链接:http://www.cnblogs.com/lidabo/archive/2011/12/29/2306126.html

  • 相关阅读:
    [Android] Android 手机下 仿 今日头条 新闻客户端
    [Android] Android Error: Suspicious namespace and prefix combination [NamespaceTypo] when I try create Signed APK
    [Android] Android Build 时报错: java.io.IOException: Could not parse XML from android/accounts/annotations.xml
    Uva 10557 XYZZY
    Uva 705
    Uva532(三维搜索)
    Uva657
    B. Bear and Three Musketeers
    1092 回文字符串(51nod)
    1050 循环数组最大子段和
  • 原文地址:https://www.cnblogs.com/xiaoyusmile/p/2391461.html
Copyright © 2020-2023  润新知