📜  最小化 DOM 访问 - JavaScript 代码示例

📅  最后修改于: 2022-03-11 15:04:26.985000             🧑  作者: Mango

代码示例1
// This is slow, it accesses the DOM element multiple times
document.querySelector('#my-element').classList.add('my-class');
document.querySelector('#my-element').textContent = 'hello';
document.querySelector('#my-element').hidden = false;

// This is faster, it stores the DOM element in a variable
const myElement = document.querySelector('#my-element');
myElement.classList.add('my-class');
myElement.textContent = 'hello';
myElement.hidden = false;