handling multiple ids in jquery

2019-01-22 04:57发布

问题:

Can multiple ids be handled like in the code

<script>
$("#segement1,#segement2,#segement3").hide()
</script>

<div id="segement1"/>
<div id="segement2"/>
<div id="segement3"/>

回答1:

Yes, #id selectors combined with a multiple selector (comma) is perfectly valid in both jQuery and CSS.

However, for your example, since <script> comes before the elements, you need a document.ready handler, so it waits until the elements are in the DOM to go looking for them, like this:

<script>
  $(function() {
    $("#segement1,#segement2,#segement3").hide()
  });
</script>

<div id="segement1"></div>
<div id="segement2"></div>
<div id="segement3"></div>