菜单
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP How to W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

Vue 教程

Vue 首页 Vue Intro Vue Directives Vue v-bind Vue v-if Vue v-show Vue v-for Vue Events Vue v-on Vue Methods Vue Event Modifiers Vue Forms Vue v-model Vue CSS Binding Vue Computed Properties Vue Watchers Vue Templates

Scaling Up

Vue:为什么、如何以及设置 Vue:第一个 SFC 页面 Vue 组件 Vue Props Vue v-for 组件 Vue $emit() Vue Fallthrough 属性 Vue Scoped 样式 Vue 本地组件 Vue Slots Vue v-slot Vue Scoped Slots Vue 动态组件 Vue Teleport Vue HTTP 请求 Vue Template Refs Vue 生命周期钩子 Vue Provide/Inject Vue 路由 Vue 表单输入 Vue 动画 Vue v-for 动画 Vue Build Vue Composition API

Vue Reference

Vue Built-in Attributes Vue Built-in Components Vue Built-in Elements Vue Component Instance Vue Directives Vue Instance Options Vue Lifecycle Hooks

Vue 示例

Vue Examples Vue Exercises Vue Quiz Vue Server Vue Certificate

Vue $el 对象


示例

使用 $el 对象更改根级别的 <div> 标签的背景颜色。

methods: {
  changeColor() {
    this.$el.style.backgroundColor = 'lightgreen';
  }
}
运行示例 »

更多示例请参见下方。


定义和用法

$el 对象表示 Vue 组件的根 DOM 节点。

Vue 应用程序挂载之前,$el 对象不存在。

如果 <template> 中只有一个 HTML 根元素

  • $el 对象就是那个根元素。
  • 可以使用 $el 对象直接操作 DOM(请参见上面的示例),但不推荐这样做。
  • 最好使用 Vue 的 ref 属性和其他 Vue 功能以声明式方式更改 DOM,因为它能使代码更一致且易于维护(请参见下面的示例 1)。

如果 <template> 中有多个 HTML 根元素

  • $el 对象将仅仅是一个 Vue 内部使用的占位符 DOM 文本节点(而不是实际的 DOM 元素)。
  • 在这种情况下,DOM 不能 使用 $el 对象进行操作(请参见下面的示例 2)。

注意:在 Vue 3 的 Composition API 中,在 <script setup>(使用 setup 函数)中无法访问 $el 属性。


更多示例

示例 1

使用 ref 属性以推荐的方式声明式地更改 <div> 标签的背景颜色,而不是使用 $el 对象。

<template>
  <div ref="rootDiv">
    <h2>Example $el Object</h2>
    <p>It is recommended, and more consistent, to use the ref attribute instead of the $el object to change the background color root DIV tag.</p>
    <button v-on:click="changeColor">Click here</button>
  </div>
</template>

<script>
export default {
  methods: {
    changeColor() {
      this.$refs.rootDiv.style.backgroundColor = 'lightgreen';
    }
  }
}
</script>
运行示例 »

示例 2

当 <template> 的根中有多个元素时,$el 对象将仅仅是 Vue 内部使用的第一个根元素的文本节点表示(而不是实际的 DOM 元素)。

在这种情况下,我们不能使用 $el 对象来操作 DOM。

<template>
  <div>
    <h2>Example $el Object</h2>
    <p>We are not able to use the $el object to change the background color of the root DIV tag when there are other tags on the root level. Open browser console to see the error generated.</p>
    <button v-on:click="changeColor">Click here</button>
  </div>
  <p>With this extra p-tag there are two tags on the root level.</p>
</template>

<script>
export default {
  methods: {
    changeColor() {
      this.$el.style.backgroundColor = 'lightgreen';
    }
  }
}
</script>

<style>
#app > div, #app > p {
  border: solid black 1px;
  padding: 10px;
}
</style>
运行示例 »

示例 3

使用 $el 对象更改 <h2> 子元素的背景颜色。

<template>
  <div>
    <h2>Example $el Object</h2>
    <p>Using the $el object to change the background color of the H2 child element.</p>
    <button v-on:click="changeColor">Click here</button>
  </div>
</template>

<script>
export default {
  methods: {
    changeColor() {
      this.$el.children[0].style.backgroundColor = 'lightblue';
    }
  }
}
</script>
运行示例 »

相关页面

Vue 教程:Vue Template Refs

Vue 教程: Vue 方法

Vue 参考:Vue 'ref' 属性

Vue 参考:Vue $refs 对象


×

联系销售

如果您想将 W3Schools 服务用于教育机构、团队或企业,请发送电子邮件给我们
sales@w3schools.com

报告错误

如果您想报告错误,或想提出建议,请发送电子邮件给我们
help@w3schools.com

W3Schools 经过优化,旨在方便学习和培训。示例可能经过简化,以提高阅读和学习体验。教程、参考资料和示例会不断审查,以避免错误,但我们无法保证所有内容的完全正确性。使用 W3Schools 即表示您已阅读并接受我们的使用条款Cookie 和隐私政策

版权所有 1999-2024 Refsnes Data。保留所有权利。W3Schools 由 W3.CSS 提供支持