jQuery:下拉;选项:第一个孩子“;作为选择器

jQuery: Dropdown "option:first-child" as selector

本文关键字:选择器 第一个 下拉 选项 jQuery 孩子      更新时间:2023-09-26

我有一个下拉列表,我想让我的jquery代码一般用于其他应用程序。所以我有一个这个字段的选择器,叫做

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

我还有一个字段#myListvar,看起来像这样:

var myDropdown = $('#myList')

现在有可能做这样的事情吗:

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

因为那样的话,我的代码就可以重复使用了。谢谢

类似的东西

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