firebug 可以告诉我对象的 javascript 名称吗?
我最近在我的页面中添加了相当多的 javascript,但都是以经典的凭感觉方式学习的。当我希望我的页面执行某些操作时,我会进行谷歌搜索和实验,直到得到我想要的结果。
我的页面中经常有一些我想要激活/停用、隐藏、显示等的对象,但是 - 错过了对 javascript 的严格介绍 - 我不知道这些东西是如何命名的。我单击打开 Firebug 的对象,希望看到该对象的一些完全限定的名称,但没有得到任何乐趣。我只需要能够执行类似 form.button.value="something" 的操作,并且需要知道该对象的名称。
有什么想法吗?
谢谢。
I've been adding quite a bit of javascript to my pages lately, but have learned it all in classic seat-of-the-pants fashion. When I want my page to do something, I google and experiment until I get the result I want.
I frequently have objects in my pages that I want to activate/deactivate, hide, show, etc., but - having missed the rigorous introduction to javascript - I've no idea how these things get named. I click on objects with firebug open, expecting to see some fully-qualified name for the object, but get no joy. I just need to be able to do something like form.button.value="something", and need to know the name of that object.
Any ideas?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
当您运行 Javascript 代码时,您拥有当前范围内的所有局部变量,因此如果您有以下代码:
...您的根对象将是 myObject,并且您通过点表示法引用后代对象。
然而,由于浏览器中 DOM 的实现,您还可以通过
this
关键字继承window
对象。事实上,对于简单的情况,即不使用闭包时,您可以省略this
关键字。因此,您只需使用函数名称即可访问顶层的 Javascript 函数。如果你想访问特定的元素,就像听起来你想要的那样,你需要学习 DOM,而不是使用你建议的语法(听起来像 20 世纪 90 年代的 Javascript)。您需要了解的根元素是
HTMLDocument
对象,可通过document
属性访问。您可以使用文档元素的getElementById
方法发现具有特定Id
(而非Name
)属性的元素。当然,如果您从一开始就为元素指定了唯一的 ID,这会很有帮助。您还可以递归地使用childNodes
集合手动迭代元素,直到找到所需的内容。或者,如果您确实无法提供 Id,也可以使用 getElementsByTagName 方法,该方法附加到 DOM 中的每个元素。例子:
Whilst you are running Javascript code, you have all the local variables in the current scope, so if you had the following code:
... your root object would be myObject, and you refer to descendent objects through the dot notation.
However, thanks to the implementation of the DOM in the browser, you also inherit the
window
object via thethis
keyword. In fact, you can leave thethis
keyword out for simple situations i.e. when you are not using closures. Therefore, you can access Javascript functions at the top level by just using the function name.If you want to access specific elements, like it sounds like you are wanting, you need to learn the DOM, and not use the syntax that you were suggesting (that sounds like 1990s Javascript). The root element you need to know is the
HTMLDocument
object, accessed via thedocument
property. You can discover elements with specificId
(notName
) attributes by using thegetElementById
method of the document element. Of course, it helps if you've given your element an unique Id from the start. You can also recursively use thechildNodes
collection to iterate manually through elements until you find what you want. Alternatively, if you really can't supply an Id, you could also use thegetElementsByTagName
method, which is attached to every element in the DOM.Example:
元素通常使用
id
属性来标识。示例:您可以使用
getElementById
方法获取对元素的引用:Elements are generally identified using the
id
attribute. Example:You can use the
getElementById
method to get a reference to an element:既然您给出了
form.button.value="something"
示例,我猜您想控制 HTML 对象。您可以为它们分配一个 id,例如:Well since you gave the
form.button.value="something"
example, I'm guessing you want to control HTML objects. You can assign them an id, e.g.: <form id="my_form"... and then get elements by id from JS, as the function is even called, getElementById().你的问题太模糊了,所以我无法给你你需要的答案。
对象
无法“激活”/“停用”,并且它们没有“名称”(它们可以具有name
属性,如果这就是您的意思) 。例如,给定:
foo
是一个变量,它的值恰好是一个对象。该对象不是变量(具有“名称”的对象)。给定:foo
现在为null
并且 foo 所持有的对象现在存储为bar
。You're question is extremely vague so I can't give you the answer you need.
Object
s can't be "activated"/"deactivated" and they don't have "names" (they can have aname
property, if that's what you mean).For example, given:
foo
is a variable that happens to have a value that is an object. The object is not the variable (object having a "name"). Given:foo
is nownull
and the object that foo held is now stored asbar
.你将无法做到这一点。变量名不与变量绑定,并且它们不在函数之间传递。您能做的最好的事情就是编写一个函数来获取字符串并将其解析回变量:
当然,这有很多问题。我建议只进行两次日志记录调用:
或者使用 printf 样式语法:
You won't be able to do that. The variable name isn't tied to the variable, and they're not passed between functions. The best you could do is write a function to take a string and resolve it back to a variable:
Of course, there's a lot of things wrong with this. I'd recommend just making two logging calls:
or, using the printf-style syntax: