在HTML5中格式化数据的语义 /最佳实践

发布于 2025-02-03 06:14:17 字数 1395 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如梦 2025-02-10 06:14:17

如果您只是简单地将数据推到屏幕上,并想“知道”数据是什么格式,那么也许您可以使用“数据”属性标记每个元素,然后在将其推向DOM之前设置值。将数据属性(例如Data-Format)设置为您喜欢的任何值后,您可以使用CSS格式化它,并且可以根据设置的值使用JavaScript来对其进行操作。

let data_format = document.querySelectorAll('[data-format]')
data_format.forEach((element) => {
    console.log(`Data Format = ${element.dataset.format}`)
})
<div>
    <li data-format="name">John Johnson</li>
    <li data-format="service-date">20K Service Due</li>
    <li data-format="model">2000 Honda Civic</li>
</div>

If you are simply pushing data to the screen and want to "know" what format the data is then maybe you can tag each element with the "data" attribute and set a value to it before you push it to the dom. Once you set the data attribute (e.g. data-format) to any value you like, you can than use CSS to format it and you can use JavaScript to manipulate it as well based on the value you set.

let data_format = document.querySelectorAll('[data-format]')
data_format.forEach((element) => {
    console.log(`Data Format = ${element.dataset.format}`)
})
<div>
    <li data-format="name">John Johnson</li>
    <li data-format="service-date">20K Service Due</li>
    <li data-format="model">2000 Honda Civic</li>
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文