Skip to content

useParentElement

获取给定元素的父元素

¥Get parent element of the given element

示例

Parent element tag: DIV

用法

¥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'

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

const tooltipWrapper = useParentElement(tooltip)

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

类型声明

typescript
export declare function useParentElement(
  element?: MaybeRefOrGetter<HTMLElement | SVGElement | null | undefined>,
): Readonly<Ref<HTMLElement | SVGElement | null | undefined>>

源代码

源代码示例文档

变更日志

No recent changes

VueUse 中文网 - 粤ICP备13048890号