Files
test-repo/docs/.vitepress/theme/components/appendix/web-basics/DomManipulator.vue
T

60 lines
2.5 KiB
Vue
Raw Normal View History

<!--
DomManipulator.vue
DOM 速体验输入标题+切换高亮类直观看到文本和样式变化
-->
2026-01-15 20:10:19 +08:00
<template>
<div class="dom-demo">
2026-01-15 20:10:19 +08:00
<div class="controls">
<div class="field">
<label>改个标题</label>
<input v-model="title" placeholder="输入新标题" />
2026-01-15 20:10:19 +08:00
</div>
<div class="field checkbox">
<label><input type="checkbox" v-model="highlight" /> 高亮模式 (class="highlight")</label>
2026-01-15 20:10:19 +08:00
</div>
</div>
<div class="card" :class="{ highlight }">
<h2 id="hero">{{ title }}</h2>
<p id="desc">这里是段落说明勾选高亮看看变化</p>
<button @click="toggleText">{{ buttonText }}</button>
</div>
2026-01-15 20:10:19 +08:00
<pre class="code"><code>// JS 改内容
const titleEl = document.getElementById('hero')
titleEl.textContent = '{{ title }}'
2026-01-15 20:10:19 +08:00
// JS 切 class
const card = document.querySelector('.card')
card.classList.toggle('highlight', {{ highlight }})</code></pre>
2026-01-15 20:10:19 +08:00
</div>
</template>
<script setup>
import { ref } from 'vue'
2026-01-15 20:10:19 +08:00
const title = ref('欢迎来到我的网站')
const highlight = ref(false)
const buttonText = ref('点我试试')
2026-01-15 20:10:19 +08:00
const toggleText = () => {
buttonText.value = buttonText.value === '点我试试' ? '再点一次' : '点我试试'
2026-01-15 20:10:19 +08:00
}
</script>
<style scoped>
.dom-demo { border: 1px solid var(--vp-c-divider); border-radius: 12px; background: var(--vp-c-bg-soft); padding: 16px; margin: 20px 0; display: flex; flex-direction: column; gap: 12px; }
.controls { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 10px; }
.field { background: var(--vp-c-bg); border: 1px solid var(--vp-c-divider); border-radius: 10px; padding: 10px; display: flex; flex-direction: column; gap: 6px; }
.checkbox { flex-direction: row; align-items: center; gap: 8px; }
input[type='text'], input[type='checkbox'] { accent-color: var(--vp-c-brand); }
.card { border: 1px solid var(--vp-c-divider); border-radius: 12px; padding: 16px; background: var(--vp-c-bg); transition: all 0.2s; }
.card.highlight { border-color: #f59e0b; box-shadow: 0 8px 18px rgba(245, 158, 11, 0.2); background: #fff7ed; }
.card h2 { margin: 0 0 8px 0; }
.card p { margin: 0 0 12px 0; color: var(--vp-c-text-2); }
.card button { background: var(--vp-c-brand); color: #fff; border: none; border-radius: 8px; padding: 8px 12px; cursor: pointer; }
.code { background: #0b1221; color: #e5e7eb; border-radius: 10px; padding: 12px; font-family: var(--vp-font-family-mono); font-size: 13px; overflow-x: auto; }
2026-01-15 20:10:19 +08:00
</style>