jQuery的元素或类LIKE选择器? [英] Element or class LIKE selector for jQuery?

查看:159
本文介绍了jQuery的元素或类LIKE选择器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论出于何种原因,我都拥有称为.main_sub1.main_sub2等的类.不必担心为什么我不能拥有.main .sub.

For whatever reason I have these classes called .main_sub1, .main_sub2 etc. Never mind why I can't have .main .sub.

jQuery是否有一种方法,可以使用属性来获取包含main的类?

Is there a way with jQuery, sort of in the way it is possible to do with attributes, to get the classes containing main?

推荐答案

使用$("[class^=main]")将选择其类名'main'开头的所有元素.看看有关选择器的jQuery文档,您可以使用很多其他变体,例如:

Using $("[class^=main]") will select all elements whose classname starts with 'main'. Take a look at jQuery docs about selectors, there are a lot of other variations you can use, for example:

  • [class*=main]将选择其类名包含'main'
  • 的元素
  • [class~=main]将选择其类名带有单词'main'(用空格分隔)的元素
  • [class$=main]将选择其类名'main'
  • 结尾的元素
  • [class*=main] will select elements whose classname contains 'main'
  • [class~=main] will select elements whose classname has the word 'main' (delimited with spaces)
  • [class$=main] will select elements whose classname ends in 'main'

这篇关于jQuery的元素或类LIKE选择器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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