jQuery选择器中的通配符 [英] Wildcards in jQuery selectors

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

问题描述

我正在尝试使用通配符来获取所有以"jander"开头的元素的ID.我尝试了$('#jander*')$('#jander%'),但是它不起作用.

I'm trying to use a wildcard to get the id of all the elements whose id begin with "jander". I tried $('#jander*'), $('#jander%') but it doesn't work..

我知道我可以使用元素的类来解决它,但是也可以使用通配符?

I know I can use classes of the elements to solve it, but it is also possible using wildcards??

<script type="text/javascript">

  var prueba = [];

  $('#jander').each(function () {
    prueba.push($(this).attr('id'));
  });

  alert(prueba);


});

</script>

<div id="jander1"></div>
<div id="jander2"></div>

推荐答案

要获取所有以"jander"开头的元素,您应该使用:

To get all the elements starting with "jander" you should use:

$("[id^=jander]")

得到那些以"jander"结尾的东西

To get those that end with "jander"

$("[id$=jander]")

另请参见 JQuery文档

这篇关于jQuery选择器中的通配符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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