最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - document.object Vs. document.getElementById() - Stack Overflow

programmeradmin4浏览0评论

What is the difference between these two syntaxes below-

document.object and document.getElementById().

I want to know when to use which syntax???

e.g.-

CODE1(Implementation with <form>)

<body onload="document.forms[1].innerHTML='hi';">//Alt: onload="document.getElementById('f1').innerHTML='hi';"
<form id=f1>
    <input />
</form>
<form id=f2>
    <input />
</form>
</body>

both the syntax in onload works the same way. But this doesn't work for the following-

CODE2(Implementation with <div>)

<body onload="document.getElementById('div1').innerHTML='hi';">//cannot use the syntax: onload="document.divs[1].innerHTML='hi';"
<div id=div1>hello</div>
<div id=div2>hello</div>
</body>

So definitely the syntax: document.object does not work with <div>-elements but works with <form>'-element. But **document.getElementById()`** works for both. So when should I use which one???

Someone please highlight the differences between the two syntaxes.

Thanx in advance...

What is the difference between these two syntaxes below-

document.object and document.getElementById().

I want to know when to use which syntax???

e.g.-

CODE1(Implementation with <form>)

<body onload="document.forms[1].innerHTML='hi';">//Alt: onload="document.getElementById('f1').innerHTML='hi';"
<form id=f1>
    <input />
</form>
<form id=f2>
    <input />
</form>
</body>

both the syntax in onload works the same way. But this doesn't work for the following-

CODE2(Implementation with <div>)

<body onload="document.getElementById('div1').innerHTML='hi';">//cannot use the syntax: onload="document.divs[1].innerHTML='hi';"
<div id=div1>hello</div>
<div id=div2>hello</div>
</body>

So definitely the syntax: document.object does not work with <div>-elements but works with <form>'-element. But **document.getElementById()`** works for both. So when should I use which one???

Someone please highlight the differences between the two syntaxes.

Thanx in advance...

Share Improve this question edited Feb 25, 2014 at 5:40 Rajesh Paul asked Oct 3, 2013 at 15:44 Rajesh PaulRajesh Paul 7,0196 gold badges43 silver badges58 bronze badges 3
  • i would use IDs whenever possible to separate the JS from the cgi. – dandavis Commented Oct 3, 2013 at 15:48
  • Can you always guarantee what position your form will be in in the DOM? – Erik Christianson Commented Oct 3, 2013 at 15:48
  • please, correct formatting – naXa stands with Ukraine Commented Feb 24, 2014 at 14:59
Add a ment  | 

3 Answers 3

Reset to default 4

document.forms is a very old method of accessing stuff, along with document.images and document.all, and possibly a few others that I don't remember.

The number one flaw in accessing document.forms[1] is simple: what if another form is added to the page, before the target one? Suddenly you have to search through all your code for references to anything, and change them.

This is where IDs e in. By only allowing one of each ID on a page, getElementById can accurately retrieve it, every time, without caring about what happens to the document in the meantime. The only change that matters is the element being removed altogether.

you can use document.object_name to find anchors, applets, embeds, forms, images, links, plugins.

to find HTML Elemets like div, span, etc. you have to use selectors. you can get this elements by element name, id, class, etc.

The easiest way to access the div in your example is via the "named access on window object" functionality (http://www.whatwg/specs/web-apps/current-work/#named-access-on-the-window-object):

Instead of

<body onload="document.getElementById('div1').innerHTML='hi';">

you can use

<body onload="div1.innerHTML='hi';">

Note that "div1" is a property of the window object, not of the document object.

发布评论

评论列表(0)

  1. 暂无评论