Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我的页面中有元素,其 id 为child_0_1_1_0. 它由四个变量组成,因此最多可以有 16 个子元素。
child_0_1_1_0
选择元素的最佳方法是什么:
权衡是在接触 DOM 与字符串操作之间。
哪一个更好?
在 JavaScript 中,接触 DOM 几乎总是最慢的事情。字符串操作将是您最好的选择。
话虽如此,大多数任务的差异将是微不足道的。不要担心过早优化你的代码;如果第 n 个子操作允许您更轻松、更干净地编写代码,那么请使用它们!如果他们在未来证明太慢,那么就解决它。