Home > Web Front-end > JS Tutorial > What is the difference between document.getElementBy('id') and $('#id')_Basic knowledge

What is the difference between document.getElementBy('id') and $('#id')_Basic knowledge

WBOY
Release: 2016-05-16 17:22:04
Original
2006 people have browsed it

The following is a brief introduction:

The first is the method of obtaining objects in js, which is relatively straightforward. If a page has multiple ids, you have to write such a long document.getElementBy("id ")

Copy code The code is as follows:

document.getElementBy( "id")


Abbreviation for document.getElementById

Personally, I recommend using the definition of document.getElementById in prototype:

Copy code The code is as follows:

function $()
{
var elements = new Array();
for (var i = 0; i < arguments.length; i )
{
var element = arguments[i];
if (typeof element == 'string')
element = document.getElementById(element);
if (arguments.length == 1)
return element;
elements.push(element);
}
return elements;
}

You can also use the following code to define:

Copy code The code is as follows:

function $(objectId) {
if(document .getElementById && document.getElementById(objectId)) {
return document.getElementById(objectId);// W3C DOM
} else if (document.all && document.all(objectId)) {
return document .all(objectId);// MSIE 4 DOM
} else if (document.layers && document.layers[objectId]) {
return document.layers[objectId];// NN 4 DOM.. note: this won't find nested layers
} else {
return false;
}
}


The second one is to get the id in jquery The method

can be found in this article

http://www.jb51.net/article/27617.htm

http://www.jb51.net/article/26702.htm
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template