How javaScript and vue get elements

Table of contents

javaScript

1 Get by ID

2 Obtain according to the tag name

3 Obtain through the new method of HTML5

Vue gets elements and sibling elements, parent elements


javaScript

1 Get by ID

get gets the element element by by

Returns an element object

document.getElementById('ID');

2 Obtain according to the tag name

get Get the element element by Tag tag Name name

Returns a collection of objects with the specified tag names stored as a pseudo-array

document.getElementsByTagName('标签名');

3 Obtain through the new method of HTML5

document.getElementsByClassName('class name') needs to be subscripted because class can have multiple elements

The querySelector('?') parameter is a css selector that can only return one element (the topmost element)

querySelectorAll('?') The parameter is a css selector, which will return all the selector collections to get a single element by subscript

 

Vue gets elements and sibling elements, parent elements

Add ref first

	<div class="" ref="divBox">

get object

let a = this.$refs.divBox

Get parent, child, sibling node method

var b = a.childNodes; 获取a的全部子节点
var c = a.parentNode; 获取a的父节点
var d = a.nextSbiling; 获取a的下一个兄弟节点
var d = a.nextElementSibling; 获取a的下一个兄弟节点
var e = a.previousSbiling;获取a的上一个兄弟节点
var e = a.previousElementSibling;获取a的上一个兄弟节点
var f = a.firstChild; 获取a的第一个子节点
var g = a.lastChild; 获取a的最后一个子节点

 

Guess you like

Origin blog.csdn.net/wanjun_007/article/details/129103715