Menu
×
   ❮   
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 的“ref”属性


示例

使用 ref 属性更改 <p> 标签内的文本

<div id="app">
  <p ref="pEl">初始文本。</p>
  <button v-on:click="changeText">更改文本</button>
</div>

<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<script type="module">
  const app = Vue.createApp({
    methods: {
      changeText(){
        this.$refs.pEl.innerHTML = "你好!";
      }
    }
  })
  app.mount('#app')
</script>
自己试试 »

请看下面的更多示例。


定义和用法

The ref 属性用于标记 <template> 中的元素,以便可以在 <script> 内的 $refs 对象中访问它们。

我们可以使用 ref 属性和 $refs 对象作为 Vue 中 getElementById()querySelector() 等纯 JavaScript 方法的替代方案。

如果使用 v-for 创建的 HTML 元素具有 ref 属性,则生成的 DOM 元素将作为数组添加到 $refs 对象中,如本示例所示。

示例

<ul>
  <li v-for="x in liTexts" ref="liEl">{{ x }}</li>
</ul>
运行示例 »

更多示例

示例 1

更改 <p> 元素内的文本。

<template>
  <h1>Example</h1>
  <p>Click the button to put "Hello!" as the text in the green p element.</p>
  <button @click="changeVal">Change Text</button><br>
  <p ref="pEl" id="pEl">This is the initial text</p>
</template>

<script>
  export default {
    methods: {
      changeVal() { 
        this.$refs.pEl.innerHTML = "Hello!";
      }
    }
  };
</script>

<style>
#pEl {
  background-color: lightgreen;
  display: inline-block;
}
</style>
运行示例 »

示例 2

第一个 <p> 标签的文本被复制到第二个 <p> 标签中。

<template>
  <h1>Example</h1>
  <p ref="p1">Click the button to copy this text into the paragraph below.</p>
  <button @click="transferText">Transfer text</button>
  <p ref="p2">...</p>
</template>

<script>
  export default {
    methods: {
      transferText() { 
        this.$refs.p2.innerHTML = this.$refs.p1.innerHTML;
      }
    }
  };
</script>
运行示例 »

示例 3

<p> 元素获取与输入字段中写入的内容相同的內容。

<template>
  <h1>Example</h1>
  <p>Start writing inside the input element, and the text will be copied into the last paragraph by the use of the '$refs' object.</p>
  <input ref="inputEl" @input="getRefs" placeholder="Write something..">
  <p ref="pEl"></p>
</template>

<script>
  export default {
    methods: {
      getRefs() { 
        this.$refs.pEl.innerHTML = this.$refs.inputEl.value;
      }
    }
  };
</script>
运行示例 »

示例 4

该按钮显示存储在 $refs 对象中的数组元素内的第三个列表元素。

<template>
  <h1>Example</h1>
  <p>Click the button to reveal the 3rd list element stored as an array element in the $refs object.</p>
  <button @click="getValue">Get the 3rd list element</button><br>
  <ul>
    <li v-for="x in liTexts" ref="liEl">{{ x }}</li>
  </ul>
  <pre>{{ thirdEl }}</pre>
</template>

<script>
  export default {
    data() {
      return {
        thirdEl: ' ',
        liTexts: ['Apple','Banana','Kiwi','Tomato','Lichi']
      }
    },
    methods: {
      getValue() { 
        this.thirdEl = this.$refs.liEl[2].innerHTML;
        console.log("this.$refs.liEl = ",this.$refs.liEl);
      }
    }
  };
</script>

<style>
pre {
  background-color: lightgreen;
  display: inline-block;
}
</style>
运行示例 »

相关页面

Vue 教程: Vue 模板引用

Vue 教程: Vue v-for 指令

Vue 参考: Vue $refs 对象


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.