n-child与最后一类冲突?

时间:2016-07-15 14:15:13

标签: html css

使用此代码段



public List<Point> GetPoints(Point p1, Point p2)
{
    List<Point> points = new List<Point>();

    // no slope (vertical line)
    if (p1.X == p2.X)
    {
        for (double y = p1.Y; y <= p2.Y; y++)
        {
            Point p = new Point(p1.X, y);
            points.Add(p);
        }
    }
    else
    {
        // swap p1 and p2 if p2.X < p1.X
        if (p2.X < p1.X)
        {
            Point temp = p1;
            p1 = p2;
            p2 = temp;
        }

        double deltaX = p2.X - p1.X;
        double deltaY = p2.Y - p1.Y;
        double error = -1.0f;
        double deltaErr = Math.Abs(deltaY / deltaX);

        double y = p1.Y;
        for (double x = p1.X; x <= p2.X; x++)
        {
            Point p = new Point(x, y);
            points.Add(p);
            Debug.WriteLine("Added Point: " + p.X.ToString() + "," + p.Y.ToString());

            error += deltaErr;
            Debug.WriteLine("Error is now: " + error.ToString());

            while (error >= 0.0f)
            {
                Debug.WriteLine("   Moving Y to " + y.ToString());
                y++;
                points.Add(new Point(x, y));
                error -= 1.0f;
            }
        }

        if (points.Last() != p2)
        {
            int index = points.IndexOf(p2);
            points.RemoveRange(index + 1, points.Count - index - 1);
        }
    }

    return points;
}
&#13;
.post { border-bottom: 1px solid black; }
.post:nth-child(2n) { background-color: #DDD; }
.post:last-of-type { border-bottom: none; }
&#13;
&#13;
&#13;

我想删除帖子3和最后一个元素之间的边框。

2 个答案:

答案 0 :(得分:1)

我终于找到了解决方案:

.post { border-bottom: 1px solid black; }
.post:nth-child(2n) { background-color: #DDD; }
.post:nth-last-child(2) { border-bottom: none; }
<div>
  <div class="post">This is post 1.</div>
  <div class="post">This is post 2.</div>
  <div class="post">This is post 3.</div>
  <div id="last">This is not a post! This is the last element here.</div>
</div>

答案 1 :(得分:0)

由于没有last-of-class,你将不得不回到Javascript,将所有被归类的div切换到顶部并将其从第一个删除。

&#13;
&#13;
div div {
  height:50px;
  background: pink;
  }

.post {
  border-top: 1px solid black;
}
.post:nth-child(2n) {
  background-color: #DDD;
}
.post:first-child {
  border-top: none;
}
&#13;
<div>
  <div class="post">This is post 1.</div>
  <div class="post">This is post 2.</div>
  <div class="post">This is post 3.</div>
  <div id="last">This is not a post! This is the last element here.</div>
</div>
&#13;
&#13;
&#13;