我应该使用 Table.cells[] 吗?
我目前正在尝试编写一些 Javascript 来循环遍历表中的元素,但想知道最佳实践是什么。我可以在表对象上调用 .cells[]
来获取表中的所有单元格,但是 W3Schools page 说这不是 W3C 标准 - 那么我应该避免它吗?
另一种选择是使用 .rows[]
获取所有行(这是 W3C 标准),然后使用 .cells[]
获取每行(同样,W3C标准)。基本上 - 坚持 W3C 标准方法有多重要?
I'm currently trying to write some Javascript to loop through the elements in a table, but was wondering what the best practise was. I could just call .cells[]
on my table object to get all of the cells in the table, but the W3Schools page says that this is not a W3C standard - should I avoid it then?
The other option is to use .rows[]
to get all the rows (which is W3C Standard), then .cells[]
on each of the rows (again, W3C Standard). Basically - how important is it that I stick to W3C Standard methods?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果存在标准,通常最好使用它:它更有可能在所有目标浏览器中得到良好支持,并且标准的广泛采用有助于防止非标准专有 API 的扩散。
这种特殊情况是显而易见的:Firefox 不支持
元素的
cells
属性,因此您无法在网络上使用它。Where the standard exists, it's generally preferable to use it: there's more chance that it's well-supported in all your target browsers and wide adoption of standards helps prevent the proliferation of non-standard proprietary APIs.
This particular case is a no-brainer: the
cells
property of a<table>
element isn't supported in Firefox, so you can't use it on the web.使用 W3C 定义的标准 API 和属性至关重要。这确保了跨浏览器兼容性、面向未来和一致性。
It is of the utmost importance to use standard APIs and properties as defined by the W3C. This ensures cross-browser compatibility, future-proofing and consistency.