编写有意义的 css .class 和 #id 名称有什么好处?
编写有意义的 css .class 和 #id 名称有什么好处?屏幕阅读器会说话来帮助用户理解标签内内容的含义和目的吗?
What is the benefit of writing meaningful css .class and #id names? Do screen readers speak to help the user understand the meaning and purpose of content inside the tags?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一般来说,它仅对开发人员/设计人员有利。
Generally-speaking, it's beneficial for the developer/designer only.
同样,正如您最近关于语义的所有问题一样,答案保持不变:
如果您的元素包含一个有意义的字段,则为它分配一个类(即使您不想对其应用 CSS)很有用,以便轻松定义该特定字段:
这样做具有以下优点:
微格式只是一个更大的例子。简而言之,它们是一组预定义的元素和属性,用于保存一组特定的数据,旨在简化第三方工具的解析。
Again, as all your recent questions on semantics, the answer stays the same:
If your element holds a meaningful field, it is useful to assign it a class (even if you do not want to apply CSS to it) just to easily define that particular field:
Doing so has the following advantages:
Microformats are just a larger example of this. Simply put, they are a set of pre-defined elements and attributes that hold a particular set of data, meant to ease parsing by third-party tools.
其他答案都很好,但我将在这里重点关注抓取/第三方工具方面。
情况1是蜘蛛,像搜索引擎一样爬行。如果他们解析您的页面并看到类似
id="username"
的内容,那么他们比id="div-style-32"
更有可能找出其中的含义>。诚然,我不确定谷歌现在是否正在做这种事情,但如果更多的人做得更好的话,它可能会这样做。情况 2 是人们编写脚本来提取 HTML 并对其进行处理,以便将其内容提取为数据。几乎任何想要做到这一点的人都可以使用任何标记,这只是它有多烦人的问题。由于语义增加,更清晰、描述更清晰的标记使抓取脚本能够更轻松地找到所需的信息。
这还包括允许用户改变网站行为的浏览器扩展或 Greasemonkey 脚本等内容。使用更清晰的标记来创建这些修改会更容易。
但是,如果您不希望人们使用客户端扩展来抓取或修改您的网站,那么从技术角度来看您无能为力。你无法阻止它,你只能让它变得更加痛苦。对于网站开发人员来说,可维护性的好处是巨大的。真的,为什么不呢?
简而言之,它使您或其他人可以更轻松地通过您的网站完成所有不同的事情。
Other answers are good, but I will focus on the scraping/third party tools aspect here.
Case 1 is spiders and crawling like search engines. If they parse your page and see something like
id="username"
, they will be more likely to figure out some meaning in that thanid="div-style-32"
. Granted, I'm not sure Google is doing this sort of thing now, but it could be if more people were better about it.Case 2 is people writing scripts to pull down the HTML and process it in order to extract its content as data. Pretty much anyone who wants to do this can with any markup, its just a matter of how annoying it is. Cleaner and more well described markup allows the scraper script to more easily find the information it needs due to it's increased semantics.
This also includes things like browser extensions or Greasemonkey scripts that allow users to alter the behavior of the site. It will be easier to create these modifications with cleaner markup.
But if you don't want people scraping or modifying your site with client side extension, there is little you can do about from a technical standpoint. You can't stop it, you can only make it more of a pain in the ass. And the benefits of maintainability for the site developers are huge. So really, why not?
In short it makes all the different things you or others could do with your site easier to do.
你这样做不是为了机器,而是为了人类。
如果我们只关心机器,我们仍然会在汇编中编码:)
You don't do it for the machines but for the humans.
If we only cared about machines we'd still be coding in assembly :)