firebug 可以告诉我对象的 javascript 名称吗?

发布于 2024-08-12 09:52:39 字数 304 浏览 10 评论 0原文

我最近在我的页面中添加了相当多的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

书信已泛黄 2024-08-19 09:52:39

当您运行 Javascript 代码时,您拥有当前范围内的所有局部变量,因此如果您有以下代码:

var myObject = New Object();
myObject.Person = New Object();
myObject.Person.Name = "Mark";
myObject.Person.Gender = "Male";
myObject.Name = "object1";

...您的根对象将是 myObject,并且您通过点表示法引用后代对象。

然而,由于浏览器中 DOM 的实现,您还可以通过 this 关键字继承 window 对象。事实上,对于简单的情况,即不使用闭包时,您可以省略 this 关键字。因此,您只需使用函数名称即可访问顶层的 Javascript 函数。

如果你想访问特定的元素,就像听起来你想要的那样,你需要学习 DOM,而不是使用你建议的语法(听起来像 20 世纪 90 年代的 Javascript)。您需要了解的根元素是 HTMLDocument 对象,可通过 document 属性访问。您可以使用文档元素的 getElementById 方法发现具有特定 Id(而非 Name)属性的元素。当然,如果您从一开始就为元素指定了唯一的 ID,这会很有帮助。您还可以递归地使用 childNodes 集合手动迭代元素,直到找到所需的内容。或者,如果您确实无法提供 Id,也可以使用 getElementsByTagName 方法,该方法附加到 DOM 中的每个元素。

例子:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
    <TITLE></TITLE>
    <META NAME="Generator" CONTENT="TextPad 4.6">
    <META NAME="Author" CONTENT="?">
    <META NAME="Keywords" CONTENT="?">
    <META NAME="Description" CONTENT="?">
    <SCRIPT LANGUAGE="Javascript">
        <!--

        function DoSummat()
        {
            var divInside = document.getElementById("Inside");
            divInside.textContent = "This text will appear inside.";
        }

        //-->
    </SCRIPT>
</HEAD>

<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#FF0000" VLINK="#800000" ALINK="#FF00FF" BACKGROUND="?">
<FORM>
    <INPUT TYPE="BUTTON" onclick="DoSummat();" NAME="MyButton" VALUE="Press Me">
</FORM>
<DIV ID="Outside">
    <DIV ID="Middle">
        <DIV ID="Inside"></DIV>
    </DIV>
</DIV>
</BODY>
</HTML>

Whilst you are running Javascript code, you have all the local variables in the current scope, so if you had the following code:

var myObject = New Object();
myObject.Person = New Object();
myObject.Person.Name = "Mark";
myObject.Person.Gender = "Male";
myObject.Name = "object1";

... 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 the this keyword. In fact, you can leave the this 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 the document property. You can discover elements with specific Id (not Name) attributes by using the getElementById 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 the childNodes 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 the getElementsByTagName method, which is attached to every element in the DOM.

Example:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
    <TITLE></TITLE>
    <META NAME="Generator" CONTENT="TextPad 4.6">
    <META NAME="Author" CONTENT="?">
    <META NAME="Keywords" CONTENT="?">
    <META NAME="Description" CONTENT="?">
    <SCRIPT LANGUAGE="Javascript">
        <!--

        function DoSummat()
        {
            var divInside = document.getElementById("Inside");
            divInside.textContent = "This text will appear inside.";
        }

        //-->
    </SCRIPT>
</HEAD>

<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#FF0000" VLINK="#800000" ALINK="#FF00FF" BACKGROUND="?">
<FORM>
    <INPUT TYPE="BUTTON" onclick="DoSummat();" NAME="MyButton" VALUE="Press Me">
</FORM>
<DIV ID="Outside">
    <DIV ID="Middle">
        <DIV ID="Inside"></DIV>
    </DIV>
</DIV>
</BODY>
</HTML>
烦人精 2024-08-19 09:52:39

元素通常使用id属性来标识。示例:

<input type="submit" id="saveButton" value="Save" />

您可以使用 getElementById 方法获取对元素的引用:

var b = document.getElementById('saveButton');
b.value = 'Saving...';

Elements are generally identified using the id attribute. Example:

<input type="submit" id="saveButton" value="Save" />

You can use the getElementById method to get a reference to an element:

var b = document.getElementById('saveButton');
b.value = 'Saving...';
兮颜 2024-08-19 09:52:39

既然您给出了 form.button.value="something" 示例,我猜您想控制 HTML 对象。您可以为它们分配一个 id,例如:

document.getElementById('my_form').button.value = 'something';

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().

document.getElementById('my_form').button.value = 'something';
似最初 2024-08-19 09:52:39

你的问题太模糊了,所以我无法给你你需要的答案。

对象无法“激活”/“停用”,并且它们没有“名称”(它们可以具有 name 属性,如果这就是您的意思) 。

例如,给定:

var foo = {};

foo 是一个变量,它的值恰好是一个对象。该对象不是变量(具有“名称”的对象)。给定:

bar = foo; foo = null;

foo 现在为 null 并且 foo 所持有的对象现在存储为 bar

You're question is extremely vague so I can't give you the answer you need.

Objects can't be "activated"/"deactivated" and they don't have "names" (they can have a name property, if that's what you mean).

For example, given:

var foo = {};

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:

bar = foo; foo = null;

foo is now null and the object that foo held is now stored as bar.

不念旧人 2024-08-19 09:52:39

你将无法做到这一点。变量名不与变量绑定,并且它们不在函数之间传递。您能做的最好的事情就是编写一个函数来获取字符串并将其解析回变量:

function printVar(name) {
    console.log(name + ": " + window[name]);
}

当然,这有很多问题。我建议只进行两次日志记录调用:

console.log("foo");
console.log(foo);

或者使用 printf 样式语法:

console.log("foo: %o", foo);

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:

function printVar(name) {
    console.log(name + ": " + window[name]);
}

Of course, there's a lot of things wrong with this. I'd recommend just making two logging calls:

console.log("foo");
console.log(foo);

or, using the printf-style syntax:

console.log("foo: %o", foo);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文