编写有意义的 css .class 和 #id 名称有什么好处?

发布于 2024-08-21 15:36:51 字数 67 浏览 8 评论 0原文

编写有意义的 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 技术交流群。

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

发布评论

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

评论(4

凤舞天涯 2024-08-28 15:36:51

一般来说,它仅对开发人员/设计人员有利。

Generally-speaking, it's beneficial for the developer/designer only.

聽兲甴掵 2024-08-28 15:36:51

同样,正如您最近关于语义的所有问题一样,答案保持不变:

这完全取决于相关实体的数据上下文。

如果您的元素包含一个有意义的字段,则为它分配一个类(即使您不想对其应用 CSS)很有用,以便轻松定义该特定字段:

<span class="username">Andrew Moore</span>

这样做具有以下优点:

  • 它可以轻松识别该字段的内容在你的代码中。
  • 它提高了可维护性。
  • 它帮助解析器和第三方应用程序获取该字段的值。

微格式只是一个更大的例子。简而言之,它们是一组预定义的元素和属性,用于保存一组特定的数据,旨在简化第三方工具的解析。

Again, as all your recent questions on semantics, the answer stays the same:

It all depends on the data-context of the entity in question.

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:

<span class="username">Andrew Moore</span>

Doing so has the following advantages:

  • It easily identifies the field's content in your code.
  • It increases maintainability.
  • It helps parsers and third-party applications to fetch this field's value.

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.

风吹短裙飘 2024-08-28 15:36:51

其他答案都很好,但我将在这里重点关注抓取/第三方工具方面。

情况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 than id="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.

征﹌骨岁月お 2024-08-28 15:36:51

你这样做不是为了机器,而是为了人类。

如果我们只关心机器,我们仍然会在汇编中编码:)

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 :)

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