简体   繁体   English

为什么我会在HTML和CSS中的属性上使用类?

[英]Why would I ever use a class over an attribute in HTML and CSS?

I'm creating a web app and need to decide on a consistent way to style my elements. 我正在创建一个Web应用程序,需要确定一种一致的方式来设置我的元素的样式。 I noticed that a lot of help online uses classes but I have generally used attributes (like the query selector [my-attr]) to style multiple elements, since I don't have to work with classList and can remove and set attributes very easily. 我注意到有很多在线帮助使用类,但是我通常使用属性(例如查询选择器[my-attr])来设置多个元素的样式,因为我不必使用classList,并且可以非常轻松地删除和设置属性。

Using no external libraries, why would people use classes to style their elements over attributes? 不使用外部库,人们为什么要使用类来根据属性设置元素样式? It seems like attributes can do everything classes can do but better, since you can also assign values to the attributes as well. 似乎属性可以完成类可以做的所有事情,但是效果更好,因为您还可以为属性分配值。 I also haven't seen any discussion on performance differences anywhere, making me believe that attributes should be more appreciated than they are in tutorials on styling pages. 我也从未见过任何关于性能差异的讨论,这使我相信属性应该比样式页面上的教程更受赞赏。

Any thoughts on instances where classes could do a better job than attributes at something would be greatly appreciated, even if the arguments are subjective and come down to some sort of preference! 即使参数是主观的并且归结为某种偏好,对类在某些方面比属性可以做得更好的实例的任何想法也将受到赞赏。

Thanks! 谢谢!

Again, this is for vanilla javascript, HTML, and CSS. 同样,这适用于原始javascript,HTML和CSS。 I understand that libraries like jQuery may make it easier to use classes, but I don't plan on using any libraries. 我知道jQuery之类的库可能会使使用类变得更加容易,但是我不打算使用任何库。

It's more consistent, is the end-all be all, truthfully. 说实话,这是更一致的,毕竟是一切。 If you'd rather use attributes, go for it. 如果您想使用属性,那就去做吧。 However it makes it just that much more difficult for anyone who has to help you out later. 但是,这给以后需要帮助的人增加很多困难。 CSS classes are designed for grouped selection and application of common styles. CSS类设计用于通用样式的分组选择和应用。 CSS just happens to also be able to select attributes as well, because it does make sense in some edge cases. CSS 恰好也能够选择属性,因为在某些情况下它确实有意义。

Generally, attributes should be reserved for anything non-style related that adds a value for a different use, be it screen-readers or variable containers for JavaScript, such as data-id="34" may let you make an asynchronous request with the ID parameter. 通常,应该为任何非样式相关的属性保留属性,这些属性会为其他用途添加值,无论是屏幕阅读器还是JavaScript的变量容器,例如data-id="34"可以让您使用ID参数。

Consider this example, it's got some simple "class" based buttons: 考虑这个例子,它有一些简单的基于“类”的按钮:

 .button { display: inline-block; border: 1px solid; padding: 4px 10px; text-decoration: none; margin-bottom: 4px; } .primary { background: currentColor; } .primary span { color: #fff; } .blue { color: #0095ee; } .red { color: #ee3300 } 
 <a href="#" class="button red">Red Button</a> <a href="#" class="button red primary"><span>Red Button</span></a> <br /> <a href="#" class="button blue">Blue Button</a> <a href="#" class="button blue primary"><span>Blue Button</span></a> 

To replicate something like this with attributes, we'll be doing something like this with some obnoxious and rather arbitrary attribute names. 为了使用属性复制类似的内容,我们将使用一些令人讨厌且相当随意的属性名称来进行类似的操作。 Doing this I actually messed up because I used the wrong attribute name and value pair in one case. 这样做实际上使我搞砸了,因为在一种情况下,我使用了错误的属性名称和值对。

 [type="button"] { display: inline-block; border: 1px solid; padding: 4px 10px; text-decoration: none; margin-bottom: 4px; } [status="primary"] { background: currentColor; } [status="primary"] span { color: #fff; } [color="blue"] { color: #0095ee; } [color="red"] { color: #ee3300 } 
 <a href="#" type="button" color="red">Red Button</a> <a href="#" type="button" color="red" status="primary"><span>Red Button</span></a> <br /> <a href="#" type="button" color="blue">Blue Button</a> <a href="#" type="button" color="blue" status="primary"><span>Blue Button</span></a> 

Does it not make more semantic sense to keep all your stylistic and group target attributes inside the class attribute? 将所有样式和组目标属性保留在class属性中是否没有更多的语义意义? I mean, that's what it was designed for. 我的意思是,这就是它的设计目的。 I suppose you could drop the parameter value and just use parameter names, but you're really defeating the purpose of attributes, considering class is a Global Attribute in and of itself. 我想您可以删除参数值并仅使用参数名称,但是考虑到class本身就是Global Attribute ,您确实在破坏属性的目的。

Here's a JavaScript example as well: 这也是一个JavaScript示例:

 let classes = document.getElementsByClassName('button'); for( i = 0, n = classes.length; i < n; ++i ){ classes[i].style.background = 'red'; } let attrs = document.querySelectorAll('[button]'); for( i = 0, n = attrs.length; i < n; ++i ){ attrs[i].style.background = 'blue'; } 
 a { padding: 10px; margin-bottom: 4px; display: inline-block; text-decoration: none; color: #fff; } 
 <a href="#" button>Attr 1</a> <a href="#" button>Attr 2</a> <a href="#" button>Attr 3</a> <br /> <a href="#" class="button">Class 1</a> <a href="#" class="button">Class 2</a> <a href="#" class="button">Class 3</a> 

Putting aside the fact that JS has immense integration with class (and id) based functions for selectors, you have to use querySelector and querySelectorAll for the attribute buttons. 撇开JS与基于类(和id)的选择器功能进行大量集成这一事实相比,您必须对属性按钮使用querySelectorquerySelectorAll

While not inherently a bad thing, (honestly I prefer querySelector over getElement(s)By… in general), but when you look at it, querySelectorAll('[button]') just does not read well, almost like I'm targeting a <button> element. 虽然天生不是一件坏事,(老实说,我更喜欢使用querySelector不是getElement(s)By… ),但是当您查看它时, querySelectorAll('[button]')读起来并不好,几乎就像我在瞄准<button>元素。 Semantically it makes it harder to read than: 从语义上讲,它比以下内容更难阅读:

getElementsByClassName('button') - Clearly getting all elements that have a button class, or even getElementsByClassName('button') -清楚地获取所有具有按钮类的元素,甚至

querySelectorAll('.button') - Since the . querySelectorAll('.button') -自. is universally understood as the "class" selector, and everyone working with HTML, CSS, and JS learns that on literally day 1 of any web development program/tutorial. 通常被理解为“类”选择器,并且使用HTML,CSS和JS的每个人都在任何Web开发程序/教程的第一天就学到了这一点。 So you're throwing a bit of a wrench into the project by removing such a fundamental piece of it. 因此,您需要删除项目中的一些基本内容,从而在项目中花了些力气。

I'm sure you've heard the phrase "Just because you can, doesn't mean you should." 我确定您已经听过“仅仅因为您可以,并不意味着您应该”这句话。 - I think that applies perfectly here. -我认为这在这里非常适用。 I mean, we actually used to use things like <font color="red">Red</font> and we moved away from it because it made more sense to group styles in a single Global Attribute. 我的意思是,我们实际上曾经使用过<font color="red">Red</font> ,因此我们放弃了它,因为在单个全局属性中将样式分组更有意义。 There's nothing stopping you, but I think it's asking for more trouble than it's worth to drop classes for arbitrary parameter names, not to mention if you have an issue and post your code for help, the first response will always be "Why are you doing that, what preprocessor are you using?" 没有什么可以阻止您的,但是我认为这比为任意参数名称删除类要值得的麻烦更多,更不用说您是否遇到问题并发布代码以寻求帮助,第一个响应始终是“您为什么这样做那,您使用什么预处理器?”

我认为这只是功能上的问题,如果您想给样式使用应该使用的类,但是如果您要修改u可以使用查询选择器,因为它不会在html上添加内联样式,并且可以缩小脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果我不需要结果列表中的CSS选择器的粒度,为什么我会想要在“实时”NodeList上使用静态NodeList / HTMLCollection? - Why would I ever want a static NodeList/HTMLCollection over a “live” NodeList if I don't need the granularity of CSS Selectors in the resulting list? 为什么我在使用QUnit编写测试时会使用expect()? - Why Would I ever use expect() When Writing Tests With QUnit? 为什么我要使用Array.toSource? - Why would I ever use Array.toSource? 为什么要使用$ rootScope? - Why would you ever use the $rootScope? 为什么我需要cancelAnimationFrame() - Why would I ever need to cancelAnimationFrame() 为什么我会使用Redux Promise Middleware而不是Redux Promise? - Why would I use Redux Promise Middleware over Redux Promise? 如何使用HTML中的数据属性用数据库中的链接填充链接和图像源? - How would I use a data-attribute in HTML to fill a link and image source with a link from a database? 根据html属性值分配CSS类 - Assigning css class based on html attribute value 为什么当我使用 addEventListener 时复选框停止工作 - why does checkbox stops working when ever i use addEventListener 为什么在发送 POST 请求时要使用 FormData 而不是 JSON? - Why would you use FormData over JSON in sending POST requests?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM