简体   繁体   English

在Javascript中为document.getElementById创建别名

[英]Make alias to document.getElementById in Javascript

How can I alias the function "document.getElementById" I've seen it done using $ 我怎么能将函数“document.getElementById”别名,我已经看到它使用$完成了

Thanks 谢谢

var $ = document.getElemenyById.bind( document );

可能是最简单/最短的方式:

function $(id) { return document.getElementById(id); };

Here's something I use in my own library. 这是我在自己的库中使用的东西。 DOM is the class and $ is a function which is a shortcut implementation of the getElementById function: DOM是类, $是一个函数,它是getElementById函数的快捷实现:

function DOM()
{
  this.$ = function(elementIDs)
  {
    var el;

    // If an array of element names is passed.
    if (arguments.length > 1) 
    {
      var elements = [];
      var length = arguments.length;
      for (var i = 0; i < length; i++)
      {
        // Call this function recursively for each passed parameter.
        elements.push(this.$(arguments[i]));
      }
      return elements;
    }

    // If a single element name is passed.
    if (typeof(elementIDs) == "string")
    {
      el = document.getElementById(elementIDs);
    }
    return el;
  }
}

Usage: 用法:

var el = new DOM().$("elementID");

You can alias it to the top level window element. 您可以将其别名为顶级窗口元素。

The new features introduced in ES6 let us improve on Chris Lloyd's answer, simplifying: ES6中引入的新功能让我们改进了Chris Lloyd的答案,简化了:

function $(id) { return document.getElementById(id); };

to: 至:

const $ = id=> document.getElementById(id);

facebook's connect-js does the following in src/core/prelude.js : facebook的connect-jssrc / core / prelude.js中执行以下操作

if (!window.FB) {
  FB = {
    // ...
    $: function(id) {
      return document.getElementById(id);
    }
    // ...
  }
}

Then, to use it, they do: 然后,要使用它,它们会:

FB.$('name-of-id');

The initial block creates the global object FB. 初始块创建全局对象FB。 They use this object, FB, as a namespace and define all their objects, properties, and functions within it. 他们使用此对象FB作为命名空间,并在其中定义其所有对象,属性和函数。 That way, it will work with other JavaScript code as long as that other code doesn't use the global object FB. 这样,只要其他代码不使用全局对象FB,它就可以与其他JavaScript代码一起使用。

function $( s ) {
    return document.getElementById( s ) ? document.getElementById( s ) : "";
}

$( "myId" );

This assumes you only want to use the dollar function (selector) to get an ID. 这假设您只想使用美元函数(选择器)来获取ID。 I didn't test that, but it should work. 我没有测试,但它应该工作。

The last time I looked at the core of jQuery, it took a string as an argument and then used a regular expression to determine what kind of selector it was. 我最后一次查看jQuery的核心时,它将一个字符串作为参数,然后使用正则表达式来确定它是什么类型的选择器。

When you've seen the $() function, it was probably some library such as jQuery or Prototype. 当你看到$()函数时,它可能是一些库,如jQuery或Prototype。 The $ function is not an alias for document.getElementById, but is a wrapper that extends the functionality of the function. $ function不是document.getElementById的别名,而是一个扩展函数功能的包装器。

To create your own wrapper, you could write a function "alias": 要创建自己的包装器,可以编写一个函数“alias”:

var alias = document.getElemenyById;

or 要么

function alias(id) { return document.getElementById(id); }

But really, I would use one of the libraries available such as jQuery or Prototype. 但实际上,我会使用其中一个可用的库,如jQuery或Prototype。

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

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