OVERLAND PARK, Kan. — Brett Parker, an elementary school teacher and rookie politician, was a Democrat running against a Republican incumbent in a Republican state that the Republican presidential candidate, Donald J. Trump, clinched by 20 percentage points.

In spite of all that, Mr. Parker will be sworn into the Kansas House of Representatives next month, one of 13 legislative seats the Democrats picked up here.

In this election year, voters across Kansas leaned firmly to the right at the federal level, but showed far more nuance when it came to their state. In parts of Kansas, they punished conservative legislators linked to Gov. Sam Brownback’s tax-cutting doctrine, instead gravitating toward moderate Republicans and Democrats like Mr. Parker who blame the governor and his legislative allies for imperiling the state’s finances and putting public schools at risk.

“Their goal was very simple, and that was to associate me with Brownback,” said James Todd, the two-term Republican lawmaker Mr. Parker challenged here in suburban Kansas City. “That obviously was effective enough to beat me.”