A jump in shares of Apple and Tesla helped the Nasdaq Composite eke out a slim gain even as other major indexes slipped.