jQuery 如何使用jQuery选择数组中的项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1274799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I select items in an array using jQuery?
提问by Matt
I have this line which successfully select all elements of class"myclass" on my page:
我在我的页面上成功选择了“myclass”类的所有元素:
var myTerms = $(".myTerm");
The above line works, myTerms is an array of all the myTerm, each of which contain "subTerms".
上面的行有效,myTerms 是所有 myTerm 的数组,每个 myTerm 都包含“subTerms”。
Then as i iterate over each myTerm, I want to use jQuery to return an array of the "subTerms" within that myTerm.
然后,当我遍历每个 myTerm 时,我想使用 jQuery 返回该 myTerm 中的“subTerms”数组。
I tried:
我试过:
var myTerms = $(".myTerm");
for (var i = 0; i < myTerms.length; i++) {
var myTerm = queryTerms[i];
subTerms = myTerm.$("subTerms");
But that's a no go. Is jQuery even intended for this or is it better to fall back to plain old java script.
但这是不行的。jQuery 甚至是为此而设计的,还是最好退回到普通的旧 Java 脚本。
EDIT
编辑
subTerm is the className of elements inside of myTerm
subTerm 是 myTerm 中元素的 className
回答by Derek Gathright
Kinda confused on what the subterms object is, but I'll give it a shot.
对 subterms 对象是什么有点困惑,但我会试一试。
$('.myTerm').each(function(){
subTerms = $(this).children();
});
** Edit **
** 编辑 **
If you want the select options, do
如果您想要选择选项,请执行
$('.myTerm').each(function(){
subTerms = $(this).children('.subTerm option');
});
回答by Jon Galloway
I'm not sure what you're asking. It sounds like you want to match all items which have the class myterm + another class in your list, e.g. <li class="myterm subterm1">
我不确定你在问什么。听起来您想匹配列表中具有 myterm 类 + 另一个类的所有项目,例如<li class="myterm subterm1">
I'd handle this in a selector. jQuery selectors can match on multiple classes if they're comma sparated, e.g. "subterm1, subterm2, subterm3" so if you join them into a string, you can filter on it.
我会在选择器中处理这个。jQuery 选择器可以匹配多个以逗号分隔的类,例如“subterm1、subterm2、subterm3”,因此如果将它们连接到一个字符串中,则可以对其进行过滤。
var result = $(".myterm").filter(myterms.join(","));
回答by gnarf
Couple of things to point out. $(".myTerm")
is not an array - its a jquery object, you can use for loops on it, but each
is much better. Inside of an each()
function this
refers to the DOMElement
. Also, if you want to search for more objects INSIDE of a jQuery object you can use $element.find('.subterm')
or $('.subTerm', element)
有几点需要指出。 $(".myTerm")
不是数组 - 它是一个 jquery 对象,您可以在其上使用 for 循环,但each
要好得多。each()
函数内部是this
指DOMElement
. 此外,如果您想在 jQuery 对象的内部搜索更多对象,您可以使用$element.find('.subterm')
或$('.subTerm', element)
$(".myTerm").each(funciton() {
// look for any .subTerm that exist underneath of us
var subTerms = $(".subTerm", this);
// do something with subTerms as an example....
var text = [];
subTerms.each(function() {
// .val() assuming its an option element
// .text() might be better.
text.push($(this).val());
});
alert(text.join(";"));
});
回答by Jaime
you can scope the second query call to just look under the found term
您可以将第二个查询调用范围限定在找到的术语下
$(".myTerms").each(function() {
var sub = $(".subTerms", this);
});
回答by zeacuss
If you don't want to change your code, then you can use the following
如果你不想改变你的代码,那么你可以使用以下
var myTerms = $(".myTerm");
for (var i = 0; i < myTerms.length; i++) {
var myTerm = queryTerms[i];
var $myTerm = $(myTerm);
subTerms = $myTerm.find("subTerms");
}