2010-08-18 103 views
5

的所有表格我想用jquery选择id开头的所有表格。jQuery选择ID为

这是我的意思是:

<table id="Tab_01"> 
    <tr> 
     <td>.... 
    <tr> 
    .... 
</table> 

<table id="Tab_02"> 
    <tr> 
     <td>.... 
    <tr> 
    .... 
</table> 
<table id="Tab_03"> 
    <tr> 
     <td>.... 
    <tr> 
    .... 
</table> 
<table id="xyz"> 
    <tr> 
     <td>.... 
    <tr> 
    .... 
</table> 

我需要什么,是选择与“Tab_”启动表,而不是使用id =“XYZ”表

我想使用此代码与此插件做出类似的导航: http://projects.allmarkedup.com/jquery_evtpaginate/demo_basic.html

任何人都可以帮助我?

非常感谢。

亚历山德罗

+0

你不能只是添加一个类到所有这些表?这可能会更快,更容易。 – 2010-08-18 15:12:23

+0

添加一个类会更好,但可以按原样完成。请参阅Ken Redler的答案。 – EndangeredMassa 2010-08-18 15:13:50

+0

[jQuery文档](http://api.jquery.com/attribute-starts-with-selector/)建议在Ken Redler的回答中使用此选择器“[...]来识别由服务器端生成的页面中的元素使用系统元素ID生成HTML的框架。“ rossale可能无法向元素添加类。 – 2010-08-18 18:02:03

回答

11

试试这个:

$('table[id^=Tab_]') 
1

使用filter()

$('table').filter(function(index){ 
    return this.id.substring(0,4) == 'Tab_'; 
}); 
2

Padolsey创造了良好的插件此。检查它here

$("table:regex(id, ^Tab)") 

这是做这件事的最佳方法。