2013-04-11 73 views
2

我的頁面中有像「abc_1_2_3_xyz」這樣的id元素。如何在JQuery中選擇以「abc」開頭並以「xyz」結尾的元素?選擇以「abc」開頭並以「xyz」結尾的元素

$('div[id^="abc"], div[id$="xyz"]'); 
+0

可能重複[這裏](http://stackoverflow.com/questions/5376431/ wildcards-in-jquery-selectors) – 2013-04-11 20:08:40

回答

5

您可以使用2個屬性選擇器。

$('div[id^="abc"][id$="xyz"]'); 
0

使用過濾器:

$('div') 
    .filter(function() { 
     return this.id.match(/^abc+xyz$/); 
    }) 
    .html("Matched!") 
; 
相關問題