Get DOM node in JavaScript

Source: Internet
Author: User

Mainly include: Document.getelementbyid,getelementsbyclassname,getelementsbytagname,getelementsbynameFunction name notation: Getelementsbyclassname, getElementsByTagName is elements!!! With s!!! getElementById's not s~~.return value:
    • getElementById returns an element that returns nullif it is not returned;
    • Getelementsbyclassname returns a htmlcollection object , similar to the NodeList, array object, if not returned [], NOT null;
Note:
    1. is not an array : can be similar to array [0] this value, but not the array, nor the method of using the array, JQ's $ (". Pink") is not an array, but can be used ES6 array.from () to become an array;
    2. non-bulk operation : Even if there is only one element, it is also an array of length 1, if you want to get the element must be added [0]. JS cannot manipulate the entire array at once, but JQ can.
varnodes = document.getElementsByTagName ("Input");//detection is not an arrayConsole.log (Array.isarray (nodes));//falseConsole.log (Array.isarray ([+]));//trueConsole.log (NodesinstanceofArray);//falseConsole.log ([i]instanceofArray);//true//using array methodsConsole.log (nodes);//(8) [input ...]Nodes.pop ();//Nodes.pop is not a functionConsole.log (nodes);
The DOM is a technique, and in XML, element elements in an HTML document are represented by the HtmlElement object.
vardiv = document.getelementsbytagname ("div");varnode = document.getElementById ("Div1");     Console.log (Node.constructor); //function Htmldivelement () {[native code]}Console.log (Div.constructor);//function Htmlcollection () {[native code]}Console.log (NodeinstanceofNode);//trueConsole.log (nodeinstanceofElement);//trueConsole.log (nodeinstanceofHtmlElement);//trueConsole.log (nodeinstanceofHtmldivelement);//trueConsole.log (typeofnode);//ObjectConsole.log (DivinstanceofHtmlcollection);//trueConsole.log (DivinstanceofNodeList);//falseConsole.log (DivinstanceofNode);//falseConsole.log (DivinstanceofArray);//falseConsole.log (typeofDIV);//Object

Get DOM node in JavaScript

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.