document.getElementsByTagName 的本地副本

发布于 2024-08-13 21:43:57 字数 220 浏览 9 评论 0原文

为什么下面的代码不起作用?

var f = document.getElementsByTagName;
var x = f('div');

我在 Chrome 中收到“TypeError:非法调用”,在 Safari 中收到“TypeError:类型错误”。我在 Firefox 中没有收到错误,但它不起作用。我还没有在 IE 或 Opera 中进行测试。

Why doesn't the following code work?

var f = document.getElementsByTagName;
var x = f('div');

I get "TypeError: Illegal invocation" in Chrome, "TypeError: Type error" in Safari. I don't get an error in Firefox, but it doesn't work. I haven't bothered testing in IE or Opera yet.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

若有似无的小暗淡 2024-08-20 21:43:57

在 Javascript 中,不存在“绑定方法”这样的东西(借用 Python 的术语,我希望你已经知道这个术语,否则解释可能需要更长的时间)。当您获取对“document.getElementsByTagName”的引用时,您只是获取对函数的引用,而不是与文档对象关联的方法。当你调用它时,“this”被设置为窗口,而不是文档,所以它不起作用。

从技术上讲,这样做会得到你想要的,但正如你可能看到的那样,这是没有意义的:(

var x = f.call(document, 'div')

这是没有意义的,因为它的可读性较差,而且不如调用 document.getElementsByTagName() 那么快。使用闭包同样没有意义。)

In Javascript there is no such thing as a "bound method" (to borrow the term from Python, which I hope you already know or the explanation may need to be longer). When you grab a reference to "document.getElementsByTagName" you are merely getting a reference to a function, not a method associated with the document object. When you call it, "this" is set to the window, not the document, so it doesn't work.

Technically doing this will get you what you want, but as you can probably see it's pointless:

var x = f.call(document, 'div')

(It's pointless because it's less readable and not as fast as calling document.getElementsByTagName(). Using a closure is similarly pointless.)

不…忘初心 2024-08-20 21:43:57

因为在 javascript 中,方法从调用它们的对象获取其 this,并且调用存储在单独变量中的方法会使 this 成为全局上下文(或窗口,在浏览器中)。这应该有效:

var f function ()
{
    return document.getElementsByTagName.apply(
        document
      , arguments
    );
}
var x = f('div');

because in javascript, methods get their this from the object on which they're called, and calling a method stored in a separate variable makes that this be the global context (or window, in browsers). this should work:

var f function ()
{
    return document.getElementsByTagName.apply(
        document
      , arguments
    );
}
var x = f('div');
夜空下最亮的亮点 2024-08-20 21:43:57

试试这个:

function f(divName){
  return document.getElementById(divName);
}

var x = f('div');   

您正在尝试使用括号来调用函数。问题是代码中的“f”是一个变量,而不是一个函数。

Try this:

function f(divName){
  return document.getElementById(divName);
}

var x = f('div');   

You are trying to call a function, by using the parenthesis. The problem is that 'f', in your code is a variable, not a function.

懒猫 2024-08-20 21:43:57

原因是需要在对象上调用 getElementsByTagName。您可以说,在函数定义内部,它使用 this 来确定要在哪个元素中查找标签。当您复制该函数然后调用它时,它将被赋予一个新的作用域,该作用域意味着 this 不指向不同的对象。要调用文档上的函数,请尝试以下操作:

var f = document.getElementsByTagName;
var x = f.call(document, "pre");
alert(x[0]);

The reason is that getElementsByTagName needs to be called on an object. You could say that inside the function definition, it uses this to figure out what element to look for the tags in. When you copy the function and then call it, it will be given a new scope, which means that this no points to a different object. To call the function on document, try this:

var f = document.getElementsByTagName;
var x = f.call(document, "pre");
alert(x[0]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文