问题描述
有关这方面的几个问题:
- 这是好习惯吗?
- 对于最后一个功能,这是一样的吗? 在Javascript(/ jQuery)?
我的意思是这样的东西:
#myElement {
position:absolute;
top:0;
left:0
}
分号。这是纯粹是因为在添加更多样式时很容易忽略,特别是如果你在一个团队中工作:
想象一下你开始:
.foo {
background-color:#F00;
color:#000< - 缺少分号
}
然后有人添加了一些样式:
.foo {
background-color:#F00;
color:#000< - 缺少分号
width:30px;
z-index:100;
}
突然,其他开发人员浪费时间知道为什么他们 width
声明不工作(或更糟的是,没有注意到它不工作)。
最明确的是,对于每个块,你会保存几个字节。这些加起来,特别是对于大样式表。不要担心这些性能增益本身,最好使用CSS Compressor,例如来自动删除
不,它是安全的,因为浏览器正确实现这部分规范。 定义了一个声明:
更重要的是:
这意味着;
用于分隔多个声明,但不需要它们来终止它们。
Javascript?
JavaScript是一个完全不同的规格的完全不同的野兽。这个特定问题已经深入地回答了。
Couple of questions concerning this:
- Is it good practice?
- Will it, on a large scale, result in better load times?
- Can it result in browsers 'breaking'?
- Is the same true for the last function in Javascript (/jQuery)?
What I mean is stuff like this:
#myElement {
position: absolute;
top: 0;
left: 0
}
It's not good practice to manually exclude semi-colons. This is purely because it's easy to overlook when adding more styles, especially if you're working in a team:
Imagine you start with:
.foo {
background-color: #F00;
color: #000 <-- missing semi-colon
}
And then someone adds some styles:
.foo {
background-color: #F00;
color: #000 <-- missing semi-colon
width: 30px;
z-index: 100;
}
Suddenly the other developer is wasting time figuring out why their width
declaration isn't working (or worse yet, doesn't notice that it's not working). It's safer to leave the semi-colons in
Most definitely, for every block, you'd save a couple bytes. These add up, especially for large style sheets. Instead of worrying about these performance gains yourself, it's better to use a CSS Compressor, such as the YUI Compressor to automatically remove the ending semi-colons for you.
No, it's safe, as browsers implement this part of the specification correctly. The CSS2 specification defines a declaration thusly:
More importantly:
This means that ;
is used to separate multiple declarations, but are not needed to terminate them.
JavaScript is a whole different beast with a completely different specification. This particular question has been answered in depth many times before on Stack Overflow.
这篇关于留出CSS块的最后一个分号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!