“We have to recognize, to win in the future, that’s the template, that’s the blueprint,” he told supporters in Mountainside. “We need to make sure people feel that if we win, we’re going to represent all New Jerseyans.”

The president loomed over every contest in New Jersey, though the dynamic shifted slightly in the House races. The Democratic candidates were quick to seize on two of Mr. Trump’s top agenda items — the newly passed federal tax law that capped the state and local tax deduction at $10,000, and the effort to repeal the Affordable Care Act — as indicative of the president’s effort to target blue states.

In the Seventh District, in the northwestern part of the state, the mere party association with Mr. Trump was too much for Representative Leonard Lance, a moderate Republican who voted against the tax bill, in his effort against Mr. Malinowski.

In the 11th District, where Mr. Frelinghuysen retired after 24 years, Ms. Sherrill focused heavily on the tax bill and standing up to Mr. Trump while running against Jay Webber, a state legislator who was among the most conservative Republican candidates in New Jersey.

Mr. Menendez’s victory was a referendum on Mr. Trump as much as it was a recognition of the senator’s extensive legislative record — the senior senator was one of the authors of the Affordable Care Act and helped steer $60 billion in federal relief funds to the state after Hurricane Sandy. Still, he was never quite able to overcome the tarnish of his corruption trial.

After the trial ended in a hung jury last November, Democratic leaders in the state quickly coalesced around him, dampening any enthusiasm for a serious primary challenge.