Change the content of a div based on selection from dropdown menu

后端 未结 6 627
粉色の甜心
粉色の甜心 2020-11-30 00:14

Is there a simple way, using JavaScript, to dynamically show/hide content in a

based on the users selection from a drop down menu? For example, if a
6条回答
  •  -上瘾入骨i
    2020-11-30 00:40

    There are many ways to perform your task, but the most elegant are, I believe, using css. Here are basic steps

    1. Listening option selection event, biding adding/removing some class to container action, which contains all divs you are interested in (for example, body)
    2. Adding styles for hiding all divs except one.
    3. Well done, sir.

    This works pretty well if there a few divs, since more elements you want to toggle, more css rules should be written. Here is more general solution, binding action, base on following steps: 1. find all elements using some selector (usually it looks like '.menu-container .menu-item') 2. find one of found elements, which is current visible, hide it 3. make visible another element, the one you desire to be visible under new circumstances.

    javascript it a rather timtoady language )

提交回复
热议问题