How can I prevent swelling in my ankles and feet?

Depends on cause. General swelling of the lower extremity can be lessened by wearing support or surgical stockings. Elevation when ever possible also can lessen it. But needs to be evaluated by doc as to the why. Swelling can be caused by problems with your heart, circulation, high blood pressure, diet, kidney or liver trouble to name a few causes.
Rest. Pregnancy is associated with mild swelling for feet and ankles especially as the pregnancy approaches term. Excessive swelling can be a symptom of pre eclampsia and you should consult your caregiver. In general swelling can be managed by keeping feet elevated as much as possible, avoid long periods of sitting or standing , wear support hose and get plenty of rest.
Get checked. Swelling in the ankles and feet could be due to congestive heart failure (chf). Get checked to see if that is what you have. Elevating you feet should help prevent swelling. Reducing salt intake also helps.
Diet, exercise, Dr. See your physician to discuss the diet, avoiding salt, checking your kidney function, the need for compression stockings, exercise/ walking, etc.