Skip to content

useParentElement

获取给定元素的父元素

¥Get parent element of the given element

示例

Parent element tag: Finding...

用法

¥Usage

当不传递参数时,它将返回当前组件的父元素。

¥When no argument is passed, it will return the parent element of the current component.

js
import { useParentElement } from '@vueuse/core'

const parentEl = useParentElement()

onMounted(() => {
  console.log(parentEl.value)
})

它还可以接受 ref 作为第一个参数。

¥It can also accept a ref as the first argument.

ts
import { useParentElement } from '@vueuse/core'
import { shallowRef } from 'vue'

// Don't forget to bind the ref to the element
const tooltip = shallowRef<HTMLElement | undefined>()

const tooltipWrapper = useParentElement(tooltip)

onMounted(() => {
  console.log(tooltipWrapper.value)
})
js
import { useParentElement } from '@vueuse/core'
import { shallowRef } from 'vue'
// Don't forget to bind the ref to the element
const tooltip = shallowRef()
const tooltipWrapper = useParentElement(tooltip)
onMounted(() => {
  console.log(tooltipWrapper.value)
})