programlearned
  • Introduction
  • js 筆記
    • Hammer.js 應用
  • node 筆記
    • 安裝與開始
    • exports 的應用
    • http 模組知識
    • Express 開始
      • 載入靜態檔案(image,css,js)
      • ejs 的使用
      • ejs layout的使用
      • ejs include的使用
      • ejs-form 的使用
      • ejs-ajax 的使用
      • route 的整理使用
    • 參考資料
  • vue 筆記
    • 安裝與開始
    • 基礎知識
      • 創建vue與輸出
      • v-bind 綁訂應用
      • v-for 與 v-if
      • v-on
      • v-class
      • 計算功能函數
      • 表單資料綁定
      • TodoList製作
    • 參考資料
  • Bootstrap 筆記
    • 相關資源網站
    • 安裝與開始
    • CSS 基礎知識
    • 容器container 應用
    • 網格系統 應用
      • 斷點
      • 版本斷點比較
      • 欄位排序
      • 欄位推移
      • 巢狀欄位
      • Flexbox 方向性
    • 間隔工具
    • 按鈕(button)
    • 不同的呈現(display)方式
    • 容器對齊方式
    • 文字(背景顏色)對齊與樣式
    • 導覽列應用
    • 參考資源
  • gitbook 製作與離線版
Powered by GitBook
On this page

Was this helpful?

  1. vue 筆記
  2. 基礎知識

v-on

應用範例

  • v-on:click="reverseText" 可簡化成:@click="reverseText"

  • 若使用@click.prevent 可以避免預設觸發(如a href="#"會回到最上面)

  • 步驟

    • 1.先架設vue物件

    • 2.使用v-model接收使用者輸入字串(需先定義變數與接收的變數)

    • 3.利用v-on使用函數reverseText進行反轉

    • 4.若會觸發預設事件(置頂事件),則使用prevent避免觸發

    • 5.將接收的變數(newText)輸出至文本上

<div id="app">
  <input type="text" class="form-control" @keydown.enter="reverseText" v-model="text">
  <button class="btn btn-primary mt-1" v-on:click="reverseText">反轉字串</button>
  <div class="mt-3">
    {{ newText }}
  </div>
  <a :href="link" class="btn btn-primary mt-1" v-on:click.prevent="reverseText">反轉字串</button>
</div>

<script>
var app = new Vue({
  el: '#app',
  data: {
    //這邊需先定義好變數名稱
    text: '',
    newText: '',
    link:'www.google.com'
  },
  methods:{
    reverseText(){
      console.log(this.text);
      this.newText = this.text.split('').reverse().join('');
    }
  }
  // 請在此撰寫 JavaScript
});
</script>
Previousv-for 與 v-ifNextv-class

Last updated 6 years ago

Was this helpful?