How to Get Elements Using tagname and id in js _ javascript tips-js tutorial

Source: Internet
Author: User
This article describes how to use tagname and id to obtain elements in js, including three methods, for more information about how to use tagname and id to obtain elements in Javascript, see the following.

Method 1: Integral MethodFirst, obtain all the elements, and then use the ai +-B method to calculate the required elements.

Method 2: Array MethodCreates an empty array in the global environment. When a loop structure is required, the elements are obtained in the loop and placed into the array.

Method 3: Function MethodWhen encountering the same group of elements, only one group of elements is operated, and all the effects are achieved by passing parameters using the function.

The Code is as follows:

 Three methods to obtain elements using tagname and id

Category name

  • Save Cancel
  • Save Cancel
  • Save Cancel

Script // [1] overall method. Obtain all elements first, and then use the ai +-B method to calculate the required element var oList = document. getElementById ('box-list'); var aInput = oList. getElementsByTagName ('input'); var aBtn = oList. getElementsByTagName ('click'); for (var I = 0; I <aBtn. length; I ++) {aBtn [I]. index = I ;}for (var I = 0; I <aInput. length; I ++) {// confirm the button aBtn [2 * I]. onclick = function () {aInput [this. index/2]. disabled = true;} // cancel the button aBtn [2 * I + 1]. onclick = function () {aInput [(this. index-1)/2]. disabled = false; aInput [(this. index-1)/2]. value = ''; console. log (1) ;}/// [2] array method. An empty array is created in the global environment. When a loop structure is required, elements are obtained in the loop, and put the array var oList = document. getElementById ('box-list'); var aIn = oList. getElementsByTagName ('lil'); var aInput = []; var aBtnY = []; var aBtnX = []; for (var I = 0; I <aIn. length; I ++) {aInput [I] = aIn [I]. getElementsByTagName ('input') [0]; aBtnY [I] = aIn [I]. getElementsByTagName ('button ') [0]; aBtnX [I] = aIn [I]. getElementsByTagName ('button ') [1]; aBtnY [I]. index = aBtnX [I]. index = I; // click the OK button aBtnY [I]. onclick = function () {aInput [this. index]. disabled = true;} // cancel the button aBtnX [I]. onclick = function () {aInput [this. index]. disabled = false; aInput [this. index]. value = ''; console. log (2) ;}/// [3] function method. When several identical elements are encountered, only one group of elements is operated, use the function to pass parameters to achieve all the effects var oList = document. getElementById ('box-list'); var aIn = oList. getElementsByTagName ('lil'); function fn (I) {var oInput = aIn [I]. getElementsByTagName ('input') [0]; var oBtnY = aIn [I]. getElementsByTagName ('button ') [0]; var oBtnX = aIn [I]. getElementsByTagName ('button ') [1]; // confirm the button oBtnY. onclick = function () {oInput. disabled = true;} // cancel the button oBtnX. onclick = function () {oInput. disabled = false; oInput. value = ''; console. log (3) ;}}for (var I = 0; I <aIn. length; I ++) {fn (I);} script

I hope this article will help you learn how to get elements from js.

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.