在jquery中:使用attr("class")值作为选择器? [英] In jquery: Use attr("class") value as a selector?

查看:501
本文介绍了在jquery中:使用attr("class")值作为选择器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个类的字段.我想使用这些类之一作为选择器,我该怎么做?

I have fields with multiple classes. I want to use one of those classes as a selector, how do I do this?

一般示例:

<input id="from"/><div class="error from">errormessage</div>
<input id="to"/>
<div><input id="when" /></div><div class="error when">errormessage</div>

因为这是from类的一个div.error,所以我想在input#from中添加一个特定的类,并且对when调用的输入也是如此.请注意,"when"包装在div中.该页面必须有几个这样的变体.因此,遍历树不是合适的解决方案.

Because ther is a div.error with the class from, I want to add a certain class to input#from and the same goes for the input called when. Note that "when" is wrapped in a div. Several variations like this are necessary for the page. Therefore, tree-traversal is not a suitable solution.

推荐答案

有关更新的问题:

认为这就是您要执行的操作,因为该元素自从成为先前的同级对象或包含在其中:

I think this is what you're after, since the element since to be a previous sibling or contained in one:

$(".error").prev().find("input").andSelf().filter("input").addClass("test");

您可以在此处进行测试.

对于上一个问题: 使用 .class选择器,如下所示:

For previous question: Use the .class selector, like this:

$(".myCass")

您还可以通过链接选择仅具有多个类的元素,例如:

You can also select elements that only have multiple classes by chaining them, for example:

$(".myCass.myClass2.myClass3")

将仅选择具有所有3 类的元素.

Would only select elements that had all 3 classes.

这篇关于在jquery中:使用attr("class")值作为选择器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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