選單
×
   ❮   
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 傳遞屬性 Vue 作用域樣式 Vue 本地元件 Vue Slots Vue v-slot Vue 作用域插槽 Vue 動態元件 Vue Teleport Vue HTTP 請求 Vue 模板引用 Vue 生命週期鉤子 Vue Provide/Inject Vue 路由 Vue 表單輸入 Vue 動畫 Vue 結合 v-for 的動畫 Vue 構建 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 模板引用

Vue 的模板引用用於引用特定的 DOM 元素。

ref 屬性被設定在 HTML 標籤上時,生成的 DOM 元素將被新增到 $refs 物件中。

我們可以使用 ref 屬性和 $refs 物件作為純 JavaScript 中 getElementById() 或 querySelector() 等方法的替代方案。

'ref' 屬性和 '$refs' 物件

帶有 ref 屬性的 HTML 標籤將被新增到 $refs 物件中,並且可以在後面的 <script> 標籤中訪問到。

示例

一個 <p> 元素的文字被更改。

App.vue:

<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>
  <p ref="pEl">This is the initial text</p>
</template>

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

下面的另一個示例展示瞭如何使用 $refs 物件將一個標籤的值複製到另一個標籤中。

示例

第一個 <p> 標籤的文字被複制到第二個 <p> 標籤中。

App.vue:

<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>
執行示例 »

從 '$refs' 獲取輸入值

我們可以深入訪問新增到 $refs 物件中的 HTML 元素,以訪問我們想要的任何屬性。

示例

一個 <p> 元素獲得與輸入欄位中寫入的內容相同的文字。

App.vue:

<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>
執行示例 »

'ref' 與 v-for 結合使用

使用 v-for 建立的帶有 ref 屬性的 HTML 元素將被新增為陣列形式到 $refs 物件中。

示例

按鈕顯示儲存在 $refs 物件內的陣列元素中的第三個列表項。

App.vue:

<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 練習

透過練習來測試自己

練習

引用用於引用特定的 DOM 元素。

請補充缺失的程式碼,以便在應用程式掛載時,“Hello World”能夠顯示在第二個 <p> 標籤中。

<template>
  <p>This is just some text.</p>
  <p >This is the initial text</p>
</template>

<script>
  export default {
    mounted() {
      this..pEl.innerHTML = "Hello World!";
    }
  };
</script>

開始練習



×

聯絡銷售

如果您想將 W3Schools 服務用於教育機構、團隊或企業,請傳送電子郵件給我們
sales@w3schools.com

報告錯誤

如果您想報告錯誤,或想提出建議,請傳送電子郵件給我們
help@w3schools.com

W3Schools 經過最佳化,旨在方便學習和培訓。示例可能經過簡化,以提高閱讀和學習體驗。教程、參考資料和示例會不斷審查,以避免錯誤,但我們無法保證所有內容的完全正確性。使用 W3Schools 即表示您已閱讀並接受我們的使用條款Cookie 和隱私政策

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