2026-01-16 19:10:21 +08:00
|
|
|
|
<!--
|
|
|
|
|
|
DomManipulator.vue
|
|
|
|
|
|
DOM 速体验:输入标题+切换高亮类,直观看到文本和样式变化。
|
|
|
|
|
|
-->
|
2026-01-15 20:10:19 +08:00
|
|
|
|
<template>
|
2026-01-16 19:10:21 +08:00
|
|
|
|
<div class="dom-demo">
|
2026-01-15 20:10:19 +08:00
|
|
|
|
<div class="controls">
|
2026-01-16 19:10:21 +08:00
|
|
|
|
<div class="field">
|
|
|
|
|
|
<label>改个标题</label>
|
|
|
|
|
|
<input v-model="title" placeholder="输入新标题" />
|
2026-01-15 20:10:19 +08:00
|
|
|
|
</div>
|
2026-01-16 19:10:21 +08:00
|
|
|
|
<div class="field checkbox">
|
2026-01-18 12:21:49 +08:00
|
|
|
|
<label
|
|
|
|
|
|
><input type="checkbox" v-model="highlight" /> 高亮模式
|
|
|
|
|
|
(class="highlight")</label
|
|
|
|
|
|
>
|
2026-01-15 20:10:19 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
2026-01-16 19:10:21 +08:00
|
|
|
|
<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
|
|
|
|
|
2026-01-16 19:10:21 +08:00
|
|
|
|
<pre class="code"><code>// JS 改内容
|
|
|
|
|
|
const titleEl = document.getElementById('hero')
|
|
|
|
|
|
titleEl.textContent = '{{ title }}'
|
2026-01-15 20:10:19 +08:00
|
|
|
|
|
2026-01-16 19:10:21 +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>
|
2026-01-16 19:10:21 +08:00
|
|
|
|
import { ref } from 'vue'
|
2026-01-15 20:10:19 +08:00
|
|
|
|
|
2026-01-16 19:10:21 +08:00
|
|
|
|
const title = ref('欢迎来到我的网站')
|
|
|
|
|
|
const highlight = ref(false)
|
|
|
|
|
|
const buttonText = ref('点我试试')
|
2026-01-15 20:10:19 +08:00
|
|
|
|
|
2026-01-16 19:10:21 +08:00
|
|
|
|
const toggleText = () => {
|
|
|
|
|
|
buttonText.value = buttonText.value === '点我试试' ? '再点一次' : '点我试试'
|
2026-01-15 20:10:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
<style scoped>
|
2026-01-18 12:21:49 +08:00
|
|
|
|
.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>
|