菜单
×
   ❮   
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 提供支持