Vue.js :单击一个按钮,浏览 DOM,找到具有特定类名的所有类,并将一个新类附加到该现有类列表中



所以,我正在使用 Axios 拉取一个 html 文件,然后将该 html 文件中的所有内容都经过<body>标签并将其附加到模板中div 内部的{{htmlData}}

看起来像这样:

<template>
<somebutton> I click on</somebutton>
<div id="some-container-name" v-html="htmlData">
{{ htmlData }}
</div>
</template>
data: function() {
return {
htmlData:''
};
},
methods: {
pullView: function(html) {
this.axios.get('http://someUrl.html').then(response => {
let corsHTML = response.data;
let htmlDoc = (new DOMParser()).parseFromString(corsHTML, "text/html");
this.htmlData = htmlDoc.documentElement.getElementsByTagName('body')[0].innerHTML;
})
},

用户可以选择单击按钮 - 然后代码搜索 dom,然后将类名附加到通过 axios 拉入的 html 文档中的每个现有you-can-edit-me类名。

这有意义吗?

由于我如何拉入这些内容,我真的没有机会使用 Vue 的 :bind 指令将任何内容绑定到此内容。我的谷歌富让我失望了,需要一些建议。是否可以编辑this.htmlData并在该对象中进行转换,然后我想重新渲染数据?

我不想用jQuery污染我的工作,想知道是否有其他人做过这样的事情?

由于您已经有一个解析的htmlDoc

for (const element of htmlDoc.querySelectorAll('.you-can-edit-me')) {
element.classList.add('additional-css-class');
}

pullView方法中,在将其分配给this.htmlData之前。

最新更新