我如何使用jQuery移动特定<option>的第二个选项?
<select class="comestibles">
<option>apple</option>
<option>banana</option>
<option>cantaloupe</option>
<option>date</option>
<option>eggplant</option>
<option>fig</option>
</select>
<select class="comestibles">
<option>apple</option>
<option>banana</option>
<option>cantaloupe</option>
<option>date</option>
<option>eggplant</option>
<option>fig</option>
</select>
Run Code Online (Sandbox Code Playgroud)
这段代码将成为eggplant第一个选项...关闭但没有雪茄.
jQuery('select.comestibles').each(function(){
$('option[value=eggplant]',this).prependTo(this);
});
Run Code Online (Sandbox Code Playgroud)
小智 6
假设只有一个选择元素......
jQuery('option[value=eggplant]').insertAfter('option:first-child')
Run Code Online (Sandbox Code Playgroud)
我还假设value你的元素实际上有属性.
问题中的代码发生了变化.现在它会......
jQuery('select.comestibles option[value=eggplant]').insertAfter('select.comestibles option:first-child')
Run Code Online (Sandbox Code Playgroud)
对于最近的变化......
jQuery('select.comestibles').each(function(){
var opts = $(this).children();
opts.filter('[value=eggplant]').insertAfter(opts.first())
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1340 次 |
| 最近记录: |