Context: Relatively large radii of some hot Jupiters observed in the ultraviolet (UV) and blue-optical are generally interpreted to be due to Rayleigh scattering by high-altitude haze particles. However, the haze composition and its production mechanisms are not fully understood, and observational information is still limited. Aims: We aim to study the presence of hazes in the atmospheres of HD 209458 b and HD 189733 b with high spectral resolution spectra by analysing the strength of water vapour cross-correlation signals across the red optical and near-infrared wavelength ranges. Methods: A total of seven transits of the two planets were observed with the CARMENES spectrograph at the 3.5 m Calar Alto telescope. Their Doppler-shifted signals were disentangled from the telluric and stellar contributions using the detrending algorithm SYSREM. The residual spectra were subsequently cross-correlated with water vapour templates at 0.70-0.96 $mu$m to measure the strength of the water vapour absorption bands. Results: The optical water vapour bands were detected at $5.2 sigma$ in HD 209458 b in one transit, whereas no evidence of them was found in four transits of HD 189733 b. Therefore, the relative strength of the optical water bands compared to those in the near-infrared were found to be larger in HD 209458 b than in HD 189733 b. Conclusions: We interpret the non-detection of optical water bands in the transmission spectra of HD 189733 b, compared to the detection in HD 209458 b, to be due to the presence of high-altitude hazes in the former planet, which are largely absent in the latter. This is consistent with previous measurements with the Hubble Space Telescope. We show that currently available CARMENES observations of hot Jupiters can be used to investigate the presence of haze extinction in their atmospheres.