需求:在頁面特定區塊輸出數字/文字,不影響其他元素。
<div id="output-area"></div>
<script>
document.getElementById('output-area').textContent = '你的數字或文字';
</script>
function renderNumber(containerId, number) {
const el = document.getElementById(containerId);
if (!el) return console.error('Container not found:', containerId);
el.textContent = number;
}
// 使用
renderNumber('output-area', Math.floor(Math.random() * 10000000).toString().padStart(7, '0'));
此模式適用於所有「局部資料綁定」場景:驗證碼顯示、計數器、即時價格、通知徽章等。