gpt4 book ai didi

javascript - 在 Vue.js 中渲染 html 标签

转载 作者:搜寻专家 更新时间:2023-10-30 22:15:19 24 4
gpt4 key购买 nike

我有以下代码:

HTML

<div id="app">
<h1>
Hello {{superscript('hello')}}
</h1>
</div>

JS

new Vue({
el: "#app",
data: {
},
methods: {
superscript(input) {
return '<sup>' + input + '</sup>'
}
}
})

我要渲染:

Hello hello

但是它呈现标签本身而不将其变成上标。 JSfiddle:http://jsfiddle.net/agreyfield91/eywraw8t/188244/

有没有办法通过 Vue.js 方法添加 html 标签?

最佳答案

您需要绑定(bind)它,而不是渲染 html:

{{ result }}  => <span v-html="result"></span>

在你的情况下:

<div id="app">
<h1>
Hello <span v-html="superscript('hello')"></span>
</h1>
<h1>
What I want it to look like: Hello <sup>hello</sup>
</h1>
</div>

关于javascript - 在 Vue.js 中渲染 html 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51430422/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com