I wonder if Trump's persistent attacks on the Khans are more calculated than we realize. Just a couple of days ago, Judge Curiel rejected Trump's attorneys' last attempt to weasel him out of a trial regarding the bogus Trump University. At this point, it appears likely that the evidence for the plaintiffs is very substantial. Curiel did postpone the trial until after the election, but between now and then, Trump is going to be running with a major fraud charge hanging over his head, and with a very substantial likelihood of conviction.
I think that he has decided that acting like an asshole will do less damage to his campaign than being, very likely, found guilty of a massive con game and theft. And it seems to have worked- the press, like some lower animal attracted to the shiniest object, is essentially ignoring this development while obsessing over a few incredibly vicious things he said; things which he knows perfectly well will hardly alienate a single voter from him.