Unable to select closest h2 element using JavaScript

前端 未结 1 1110
故里飘歌
故里飘歌 2021-01-26 09:30

I would like to be able to select the closest h2 heading to the fieldset that contains the pizza selection options and set the text for the heading using jQuery. I have so far b

1条回答
  •  情话喂你
    2021-01-26 10:27

    closest() does only work to get the fieldset for your control. If you know that the h2 comes after the fieldset on the same level, you could do:

    $(this).closest('fieldset').nextAll('h2').first();
    

    But it depends on what you mean with 'closest'. You might want to have a look at the jQuery tree traversal functions.

    0 讨论(0)
提交回复
热议问题