简体   繁体   English

document.getElementById()VS。 getElementById()

[英]document.getElementById() VS. getElementById()

It is common for me to register javascript functions for certain events by doing something like: 我通常会通过执行以下操作来为某些事件注册javascript函数:

myBtn.Attributes.Add("onClick", "Validate(getElementById('"+txtFirstName.ClientID + "'));");

I've always used getElementById by itself, or in other words, sans document being prepended to it. 我一直getElementById使用getElementById ,换句话说,没有文档在其前面。 But I'm having pages break on me lately when I try to use getElementById instead of document.getElementById . 但是最近当我尝试使用getElementById而不是document.getElementById时,页面中断了。 Why is this? 为什么是这样? Oddly, I have a website where one page allows me to use just getElementById , but another other page throws a javascript error because it can't find the element if I do just getElementById , and it'll only work if I do document.getElementById . 奇怪的是,我有一个网站,其中一个页面仅允许我使用getElementById ,而另一页面则引发了javascript错误,因为如果我仅执行getElementById就无法找到该元素,并且只有在我执行document.getElementById它才能工作。

Anyone know why this is? 有人知道为什么吗? Should I be using document.getElementById everywhere, regardless of whether it works without the document prefix? 我是否应该在所有地方都使用document.getElementById ,无论是否没有文档前缀也可以使用?

EDIT: Could it have anything to do with the fact that one page is using AJAX and the other isn't? 编辑:可能与一页正在使用AJAX而另一页却没有的事实有关吗?

When you use getElementById() and it works that mean that the function where it's called is running on the context of the document, that's is this == document. 当您使用getElementById()并且它起作用时,意味着被调用的函数正在文档的上下文中运行,即== document。

So, you should ALWAYS use document.getElementById to avoid that kind of errors. 因此,您应该始终使用document.getElementById来避免此类错误。

Anyway, I would even stop using getElementById altogether and start using JQuery , i'm sure you'll never regret it. 无论如何,我什至会完全停止使用getElementById并开始使用JQuery ,我敢肯定您永远不会后悔。

Your code would look something like this if you used JQuery: 如果使用JQuery,您的代码将如下所示:

$("#myBtnID").click(function () { Validate($("#myTextboxID"))});

Any function or variable you access without an owning object (ex: document.getElementById) will access the property from window. 您在没有所有者对象的情况下访问的任何函数或变量(例如:document.getElementById)都将从窗口访问该属性。

So getElementById is actually window.getElementById, which isn't natively defined (unless you defined it before (ex: getElementById = document.getElementById). 因此,getElementById实际上是window.getElementById,它不是本地定义的(除非您之前已定义它(例如:getElementById = document.getElementById))。

You should only use document.getElementById (even if I'd recommend using libraries like prototype or jquery to be able to use the $ sign). 您应该只使用document.getElementById (即使我建议使用诸如prototype或jquery之类的库也可以使用$符号)。

If you are able to use getElementById on its own, it's just because the browser you're using is doing some kind of trick to get it to work, but the correct way is to use the document variable. 如果您能够自己使用getElementById,那仅仅是因为您使用的浏览器正在采取某种技巧使其正常工作,但是正确的方法是使用document变量。

You should use the full document.getElementById() . 您应该使用完整的document.getElementById() If you find that too verbose, you could use jQuery: 如果您觉得太冗长,可以使用jQuery:

$('#' + id)

or you could create an alias at the top of your script: 或者,您可以在脚本顶部创建别名:

var byID = document.getElementById;

I dont really know how to explain it but its because the getElementById() finds an element in the html structure of a page. 我真的不知道如何解释它,但是因为getElementById()在页面的html结构中找到了一个元素。 Some browsers know that by default you want to search the document, but other browsers need that extra guidance hence document. 有些浏览器知道默认情况下您要搜索文档,但是其他浏览器需要该文档的额外指导。

The correct way is indeed document.getElementById() . 正确的方法确实是document.getElementById()

The reason (speculation) it might work by itself is that depending on where you use it the current context may in fact be the document object, thus inexplicitly resulting in document.getElementById(). 它可能单独工作的原因(推测)是,取决于您在哪里使用它,当前上下文实际上可能是文档对象,因此导致了document.getElementById()的明确说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM