js Form.elements[i]的使用實例

function pdf(){
    //一個html裏面可能存在多個form,因此document.form[0]指的是第一個form,document.form[1]返回就是第二個form,若是沒有第二個form,則返回undefined
   //就至關於:document.getElementsByTagName(form)[0]javascript

form = document.getElementsByTagName("form")[0];
    with (form) {
        var checked = 0;
        var checkedObj = null;
        for ( var i = 0; i < elements.length; i++) {// 歷遍form下的每一個元素,elements.length 就是form 元素的個數,
            if (elements[i].name == "checkBoxs") {////檢查每一個元素的name屬性是否是 checkBoxs.
                if (elements[i].checked) {
                    checked++;
                    checkedObj = elements[i];
                }
            }
        }
    }
    if (checked == 1) {
        url = basePath + "/dtms/process/pdfEdit.do?flag_id=" + checkedObj.value;
        var w = screen.availWidth;
        var h = screen.availHeight;
        window .open(
                        url,
                        "win",
                        "fullscreen=0,toolbar=1,location=1,directories=1,status=0,menubar=0,scrollbars=1,resizable=1,width="
                                + w + ",height=" + h + ",top=0,left=0", false);html

  /*打開一個新窗口java

  Window.open()方法參數詳解http://www.cnblogs.com/sofiawang/articles/1544631.html):window.open('page.html', 'newwindow', 'height=100, width=400, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no')   //該句寫成一行代碼
   參數解釋:
      window.open 彈出新窗口的命令;
  'page.html' 彈出窗口的文件名;
  'newwindow' 彈出窗口的名字(不是文件名),非必須,可用空''代替;
  height=100 窗口高度;
  width=400 窗口寬度;
  top=0 窗口距離屏幕上方的象素值;
  left=0 窗口距離屏幕左側的象素值;
  toolbar=no 是否顯示工具欄,yes爲顯示;
  menubarscrollbars 表示菜單欄和滾動欄。
  resizable=no 是否容許改變窗口大小,yes爲容許;
  location=no 是否顯示地址欄,yes爲容許;
  status=no 是否顯示狀態欄內的信息(一般是文件已經打開),yes爲容許;app

  */
    } else if (checked > 1) {
        alert("你只能選擇一個選項!");
    } else {
        alert("你必須選擇一個選項!");
    }
}函數

 

當中with的用法以下:工具

當你有一個對象的多個屬性或者方法須要操做時,就能夠使用with

好比
<body>
test
<script type="text/javascript">
var o=document.createElement("div");
with(o){
style.cursor="pointer";
style.zIndex="100";
innerHTML="aaaa";
}
document.body.appendChild(o);
</script>
</body>
上面的代碼至關於

<body>
test
<script type="text/javascript">
var o=document.createElement("div");
o.style.cursor="pointer";
o.style.zIndex="100";
o.innerHTML="aaaa";
document.body.appendChild(o);
</script>
</body>

因此with 用於簡化 代碼 操做。
性能

with的弊端:有輕微的性能損失,若是隻是在順序代碼中調用了with幾回,這種性能能夠忽略不計。url

  1. JavaScript的嚴格模式不支持with。spa

  2. 容易引發歧義。若是method4這個函數在obj5中不存在,不用with的話,JS直接崩潰,容易查錯,可是用了with,有可能一個外部的名爲method4就被調用了,可能引發錯誤蔓延。orm

  3. 若是有上面的問題的話,object名字很長,能夠用下面的方法代替:

    var tempObj = obj1.obj2.obj3.obj4.obj5;

    temoObj.method1();

    temoObj.method2();

    temoObj.method3();

    temoObj.method4()

相關文章
相關標籤/搜索