Are add() and appendChild() the same for select DOM objects?

后端 未结 3 580
暗喜
暗喜 2020-12-10 08:14

I\'m working on a small web application that changes the contents of a select drop-down.

I was wondering if appendChild() and add() both accomplish the same task on

3条回答
  •  天涯浪人
    2020-12-10 09:04

    In this case, I believe they would accomplish the same task.

    add( ) is provided specially via the HTMLSelectElement interface specifically to add options to a select element and has a few extra options (such as an optional index at which to add the new option, etc.).

    appendChild( ) will give you a straight append of the child node.

    Watch out for different implementations of both methods cross-browser: I think that would be your biggest pain point. IIRC, the IEs would cause problems when appending children to selects, so you might want to add if that method exists and fall back on appendChild.

提交回复
热议问题