選單
×
   ❮   
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 Attributes Vue 作用域樣式 Vue 本地元件 Vue Slots Vue v-slot Vue Scoped Slots 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 v-html 指令


示例

使用 v-html 指令輸出一個包含 <ol><li> 標籤的列表。

<div id="app">
  <div>{{ htmlContent }}</div>
  <div v-html="htmlContent"></div>
</div>
自己動手試一試 »

更多示例請參見下方。


定義和用法

v-html 指令用於將 HTML 標籤和文字插入到元素中。

如果您嘗試使用文字插值(使用花括號 {{ }})輸出 HTML 標籤,結果將只是一段文字字串。請參見上面的示例。

在單檔案元件 (SFC) 中使用 <style scoped> 定義的作用域樣式不會影響來自 v-html 指令的 HTML。請參見下面的第一個示例。

要在 SFC 中為透過 v-html 包含的 HTML 實現作用域樣式,我們可以使用 CSS 模組和 <style module>。請參見下面的第二個示例。

注意: 使用者可以透過某種方式控制透過 v-html 包含的內容的頁面,存在跨站指令碼 (XSS) 攻擊的風險。


更多示例

示例 1

使用作用域樣式,對於透過 v-html 包含的 HTML,樣式不起作用。

這個問題在下一個示例中得到了解決。

<template>
  <h1>Example</h1>
  <p>When using scoped styling, styling for HTML included with the 'v-html' directive does not work.</p>
  <p><a href="showvue.php?filename=demo_ref_v-html2_2">See the next example</a> for how we can fix this by using CSS Modules.</p>
  <div v-html="htmlContent" id="htmlContainer"></div>
</template>

<script>
export default {
  data() {
    return {
      htmlContent: "<p>Hello from v-html</p>"
    }
  }
};
</script>

<style scoped>
  #htmlContainer {
    border: dotted black 1px;
    width: 200px;
    padding: 10px;
  }
  #htmlContainer > p {
    background-color: coral;
    padding: 5px;
    font-weight: bolder;
    width: 150px;
  }
</style>
執行示例 »

示例 2

使用 CSS Modules 和 <style module>,而不是 <style scoped>,樣式是作用域化的,並且樣式對於透過 v-html 包含的 HTML 有效。

上一個示例中的問題現在已得到解決。

<template>
  <h1>Example</h1>
  <p>Scoped styling for HTML included with the 'v-html' directive now works by using CSS Modules with 'style module', instead of 'style scoped'.</p>
  <div v-html="htmlContent" :id="$style.htmlContainer"></div>
</template>

<script>
export default {
  data() {
    return {
      htmlContent: "<p>Hello from v-html</p>"
    }
  }
};
</script>

<style module>
  #htmlContainer {
    border: dotted black 1px;
    width: 200px;
    padding: 10px;
  }
  #htmlContainer > p {
    background-color: coral;
    padding: 5px;
    font-weight: bolder;
    width: 150px;
  }
</style>
執行示例 »

相關頁面

Vue 教程:文字插值


×

聯絡銷售

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

報告錯誤

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

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

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