我想要一件外衣Element https://developer.mozilla.org/en-US/docs/Web/API/Element在 Vue 中,使用getElementById() https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById:
new Vue({
el: "#vue",
data: {
helloElement: document.getElementById('hello')
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.2/vue.js"></script>
<div id="hello">
hello
</div>
<div id="vue">
{{helloElement}}
</div>
这给我带来了空虚Element
,但不是一个null
- 这表明查询部分成功(发现了一些东西)。
这实际上是一个针对我面临的更普遍问题的 POC:获得外部的能力Element
(比如说,一个容器div
)来自 Vue 组件(这给我带来了null
, which 文档说 https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById#Return_Value作为未找到该元素时的响应)。
是否可以从 Vue 对象或组件内部查询外部Element
存在于 DOM 中?
正如您所看到的,这是可能的。但请注意,如果您想要这样的东西,您的应用程序可能设计得很糟糕,并且您创建了 XSS 攻击的入口点。
new Vue({
el: "#vue",
data: {
helloElement: document.getElementById('hello').outerHTML
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.2/vue.min.js"></script>
<div id="hello">
hello
</div>
<div id="vue" v-html="helloElement"></div>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)