Skip to content

useParentElement

获取给定元素的父元素

¥Get parent element of the given element

示例

Current element tag: Finding...

Parent element tag: Finding...

用法

¥Usage

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

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

vue
<script setup lang="ts">
import { 
useParentElement
} from '@vueuse/core'
const
parentEl
=
useParentElement
()
onMounted
(() => {
console
.
log
(
parentEl
.
value
)
}) </script>

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

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

vue
<script setup lang="ts">
import { 
useParentElement
} from '@vueuse/core'
import {
shallowRef
} from 'vue'
const
tooltip
=
shallowRef
<HTMLElement | undefined>()
const
tooltipWrapper
=
useParentElement
(
tooltip
)
onMounted
(() => {
console
.
log
(
tooltipWrapper
.
value
)
}) </script> <template> <
div
>
<
p
ref
="
tooltip
" />
</
div
>
</template>