Individuals with anxiety often exhibit cognitive flexibility impairment; however, the neural underpinnings of this cognitive impairment remain unclear. In this study, 45 participants were instructed to complete a task-switching assessment of shifting function by EEG technology, and 200 participants were included in microstate analysis to study why cognitive flexibility is impaired and the neuromechanism. Behaviorally, a positive correlation between trait anxiety scores and set shifting cost was found. At the EEG level, there was a positive correlation between trait anxiety scores and frontal P2 peaks under the shifting condition, which was related to the activation of the stimulus-response associations by attention. Furthermore, microstate analysis was used to analyze EEG functional networks, and TA scores had significant positive correlations with the Occurrence of class D and the Contribution of class D, which was related to the dorsal attention network. These results provided direct neuroelectrophysiological evidence that trait anxiety impairs cognitive flexibility when shifting is required.