使用jQuery从列表中选择元素 [英] Select Element From List by Index with jQuery

查看:63
本文介绍了使用jQuery从列表中选择元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<form id="foo">
    <input></input>
    <input></input>
    <input></input>
</form>

我想做

document.getElementById("foo").getElementsByTag("input")[1];

但是在jQuery中.我想通过索引选择#foo下的某个对象.

But in jQuery. I want to select a certain object under #foo by an index.

这是我对如何执行此操作的第一个猜测:

This is my first guess as to how to do this:

$('#foo input[1]').val("BlahBlah");

我认为CSS也是如此.

I think it would be the same in CSS too.

推荐答案

您可以这样做:

$('#foo input').eq(1).val("BlahBlah");

这将为您提供第二个输入.如果要使用第一个,请将1更改为0..eq()函数基于0.

That will give you the second input. If you want the first, change the 1 to a 0. The .eq() function is 0 based.

这篇关于使用jQuery从列表中选择元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆