如何使用jQuery移动< option>成为第二要素? [英] How can I use jQuery to move an <option> to be the second element?
本文介绍了如何使用jQuery移动< option>成为第二要素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用jQuery移动特定的<option>
作为第二个选项?
How can I use jQuery to move a specific <option>
to be the second 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>
此代码将使eggplant
成为第一个选择...关闭但没有雪茄.
This code will make eggplant
the first option... close but no cigar.
jQuery('select.comestibles').each(function(){
$('option[value=eggplant]',this).prependTo(this);
});
推荐答案
假设只有一个选择元素...
Assuming there's only one select element...
jQuery('option[value=eggplant]').insertAfter('option:first-child')
我还假设您的元素上实际上具有value
属性.
I also assume that you actually have value
attributes on your elements.
问题中的代码已更改.现在应该是...
The code in the question changed. Now it would be...
jQuery('select.comestibles option[value=eggplant]').insertAfter('select.comestibles option:first-child')
对于最新更改...
For the most recent change...
jQuery('select.comestibles').each(function(){
var opts = $(this).children();
opts.filter('[value=eggplant]').insertAfter(opts.first())
});
这篇关于如何使用jQuery移动< option>成为第二要素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文