The solution presented in How to highlight specific entries in a tocloft-formatted ToC did not work for colored background. There is a problem when using color because the colored rectangles are always drawn in front of the text.
I added also a color change from section to section. Here's a minimal script to generate the problem:
\documentclass{article}
\usepackage{tocloft}
\usepackage{xcolor}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\newcounter{seccntr}
\setcounter{seccntr}{-1}
\newcommand*{\hnode}[1]{%
\tikz[remember picture] \node[minimum size=0pt,inner sep=0pt,outer sep=4.5pt] (#1) {};}
% create a node at the beginning of the section entry
\renewcommand{\cftsecfont}{\hnode{P1} \bfseries\Large}
\renewcommand{\cftsecpagefont}{\bfseries}
% create a node at the end of the section number and draw the gray box
\renewcommand{\cftsecafterpnum}{%
\stepcounter{seccntr} %
\ifcase\value{seccntr}%
\hnode{P2} \tikz[remember picture,overlay] \draw (P1.north west) [line width={17pt}, red,opacity=0.3] -- (P2.north east); %----- 0 --
\or \hnode{P2}\tikz[remember picture,overlay] \draw (P1.north west) [line width={17pt}, green,opacity=0.4] -- (P2.north east);%---- 1 --
\or \hnode{P2}\tikz[remember picture,overlay] \draw (P1.north west) [line width={17pt}, yellow,opacity=0.5] -- (P2.north east);%--- 2 --
\or \hnode{P2}\tikz[remember picture,overlay] \draw (P1.north west) [line width={17pt}, blue,opacity=0.6] -- (P2.north east);%---- 3 --
\or \hnode{P2}\tikz[remember picture,overlay] \draw (P1.north west) [line width={17pt}, orange,opacity=0.7] -- (P2.north east);%-- default
\else \hnode{P2}\tikz[remember picture,overlay] \draw (P1.north west) [line width={17pt}, gray,opacity=0.8] -- (P2.north east);%-- default
\fi %
} %
\begin{document}
\tableofcontents
\section{First Section}
\subsection{A subsubsection}
\subsection{A subsubsection}
\section{Second Section}
\subsection{A subsubsection}
\end{document}
Can anyone help to put the colored boxes in the background?
Also adding
\begin{pgfonlayer}{background}\tikz .... \end{pgfonlayer}
didn't help, because it is just the background of the picture.

P2is placed after the page number, this can only be done after the page number itself has been placed, otherwise we don't know where it is. By the way, you have a spurious space in your first case, causing the right hand side of the rectangles to not align properly, this can be seen in the image in @MartinScharrer's comment. – Roelof Spijker Nov 04 '11 at 12:20