document.getElementById(“selAge”) Vs document.myForms.selAge

后端 未结 4 2005
予麋鹿
予麋鹿 2020-12-22 04:05

What is difference between

document.getElementById(“selAge”) 
document.myForms.selAge

When to use which?

4条回答
  •  离开以前
    2020-12-22 05:02

    document.getElementById() is the recommended way to get reference of elements. It's easier to use and also would not require any changes if you decide to change the Form name or Id. However, as it traverses the entire DOM tree, it is usually slower than the document.forms notation of referencing objects, so take this into account when doing a lot of such look-ups.

    document.forms is also harder to use, as you need you to know the entire path to the object beginning from the document element.

提交回复
热议问题