简体   繁体   中英

jQuery: Dropdown “option:first-child” as selector

I have a dropdown and I want to make my jquery code generally for other applications. So I have a selector for this field which is called

$('#myList option:first-child').attr('selected', 'selected').

I also have a var for the field #myList which looks like this:

var myDropdown = $('#myList') .

Is it now possible to do someting like this:

myDropdown.option:first-child.attr('selected', 'selected') .

Because then, my code would be reusable. Thanks!!

Something like

var myDropdown = $('#myList');

$('option:first-child',myDropdown).prop('selected', true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM