我是CSS伪类x-of-type
系列的狂热用户:
:first-of-type
:last-of-type
:nth-of-type
:nth-last-of-type
我通常非常擅长确保任何系列的类似元素(列表项等)完全按照我的意愿显示。
然而,在过去一小时内,我一直坚持如何使用单一样式声明来实现以下结果:
border-bottom
border-bottom
border-bottom
border-bottom
我已经实现了我的需要,使用:
.item {
border-bottom:6px solid rgb(227,227,227);
}
.item:last-of-type {
border-bottom:none;
}
但为了简洁起见,我仍然希望通过单一声明获得相同的结果。
问题:
为什么赢了
.item:nth-of-type(n-1) {
border-bottom:6px solid rgb(227,227,227);
}
或
.item:nth-last-of-type(n+1) {
border-bottom:6px solid rgb(227,227,227);
}
工作?
是不是因为n
只是一个无限的数字列表,包括页面上目标元素数量之前和之后的数字?
如果是这样,我如何在单一声明中声明"除最后一项" 之外的所有内容?
(事先道歉,如果它真的很明显,而我却没有注意到它......)
以下是我正在使用的HTML:
<aside>
<img src="" title="" alt="" />
<span class="something-else">Blah</span>
<span class="item">Item Details</span>
<img src="" title="" alt="" />
<span class="something-else">Blah</span>
<span class="item">Item Details</span>
<img src="" title="" alt="" />
<span class="something-else">Blah</span>
<span class="item">Item Details</span>
<img src="" title="" alt="" />
<span class="something-else">Blah</span>
<span class="item">Item Details</span>
</aside>
答案 0 :(得分:3)
试试这个选择器:
item
选择类:not
的所有元素,即:last-of-type
:not()
。
请注意,您可以将/* This is valid */
p:not(.classA):not(.classB):not(.classC) {
/* ... */
}
/* This isn’t: */
p:not(.classA.classB.classC) {
/* ... */
}
与任何单个选择器一起使用。
/* First part to populate example data */
// The universo of people
var people = [];
// The single person item
var person = {};
var count;
// The lenth of the array
var len = 250;
// We're gonna create an array with the format
// [
// {
// "id": 1,
// "origin": "a",
// "destination": "b"
// },
// ...
// ]
for ( count = 1; count <= len; count ++) {
var rnd = Math.ceil(Math.random() * 25);
rnd = String.fromCharCode(97 + rnd)
person = {};
person.id = count;
person.origin = rnd;
rnd = Math.ceil(Math.random() * 25);
rnd = String.fromCharCode(97 + rnd)
person.destination = rnd;
people.push( person );
}
// Here people is the universe of data
console.log ( people );
// Here we get a random person in people
// this person for run the test
rnd = Math.ceil(Math.random() * len);
person = people[rnd];
console.log( person );
// Next is the actual algorith
// The path is the array to return, obviously starting with person
path = [person];
// Route will the actual route of change to move the people and get where they want
// we call findMyPath a recursive function
route = findMyPath( person, person, path, people );
console.log('Done');
console.log( route );
/**
* This recursive function actually implements the algorithm
*/
function findMyPath(source, currentItem, path, universe) {
// The algorithm is:
// Reverse query:
// Instead of find the people that is where I want to go,
// find the people that want to go where I am
// if at least one is where I want to go, then I'm done
// if not, then repeat recursively for every finding
// Holds the people that wanto to go where I am
var findings = [];
// Loop the universe
for ( i = 0; i< universe.length; i++ ) {
// tmp is the current item in the universe
var tmp = universe[i];
// If he/she want to go where I am
if ( currentItem.origin == tmp.destination ) {
// It's a finding!
findings.push( tmp );
// If he/she is where I want to go
if ( source.destination == tmp.origin ) {
// It's a change complete, I'm done now
console.log( 'Found the route of changes!' );
path.push( tmp );
return path;
}
}
}
// If we get here, we don't find a trade course yet,
// the repeat recursively for all findinds
for ( i = 0; i < findings.length; i++ ) {
path.push( findings[0] );
return findMyPath(source, findings[0], path, universe);
} // end for
} // end function findMyPath
答案 1 :(得分:2)
是不是因为
n
只是一个无限的数字列表,包括页面上目标元素数量之前和之后的数字?
是的,这就是:nth-of-type(n-1)
似乎与每个元素匹配的原因:n
计数无数次。因此,当n
为1加上该元素类型的子项总数时,最后一个类型仍将匹配。换句话说,任何n-b
(或b
的{{1}})的n+b
表达式等同于b <= 1
(本身n
),是保证匹配。
n-0
已接近,但之所以没有做到你想要的是因为:nth-last-of-type(n+1)
从零开始计数,所以当n
为零时,n
匹配最后一种类型。
如果是这样,我如何在单个声明中声明“除了最后一项之外的所有内容”?
你有两种方法。你接近的是n+1
。另一个是更清晰的:nth-last-of-type(n+2)
。