Upon granting a divorce, the court may give the custody and education of the children of the marriage to either father or mother, as may seem right and proper, having regard to the moral character and prudence of the parents and the age and sex of the children; and pending the action, may make such orders in respect to the custody of the children as their safety and well-being may require. But in cases of abandonment of the husband by the wife, he shall have the custody of the children after they are seven years of age, if he is a suitable person to have such charge.