<form id="foo">
<input></input>
<input></input>
<input></input>
</form>
我想做:
document.getElementById("foo").getElementsByTag("input")[1];
但是在 jQuery 中。我想通过索引选择#foo下的某个对象。
这是我对如何执行此操作的第一个猜测:
$('#foo input[1]').val("BlahBlah");
我认为 CSS 中也是一样的。
最佳答案
你可以这样做:
$('#foo input').eq(1).val("BlahBlah");
这将为您提供第二个输入。如果您想要第一个,请将 1 更改为 0。.eq()
函数是基于 0 的。
关于javascript - 使用 jQuery 从列表中按索引选择元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8138969/