<template>
<div class="wrap">
<div>内容</div>
</div>
</template>
<script>
export default {
data() {
return {};
},
mounted() {
//复制dom:控制台 => Element => 鼠标选中元素右击 => Copy => Copy JS path
//(这里dom为:#app > div > div)
this.AddStyle(`#app > div > div{background: #d6e0e8;color:red;}`);
},
methods: {
AddStyle(a) {
let style = document.createElement("style");
let body = document.querySelector("head");
body.append(style);
let b = a.replace(/;/g, "!important;");
let html = b;
style.innerHTML = html;
},
},
};
</script>
<style scoped>
</style>
Vue - 使用 js 方式将 css 样式添加到 head 标签中
于 2020-09-25 09:25:36 首次发布