2012-04-26 42 views
0

我想知道如何檢查我的href是否包含點擊函數中的字符串?我嘗試不同的方式,並已經看過其他類似的問題,但我似乎無法得到它的工作。我已經試過包含,也正則表達式..下面的代碼:jquery檢查href的字符串點擊?

$.each($ul.find('a'),function(){ 
    $(this).click(function(e){ 
    e.preventDefault(); 
    if () { // this is where i've been trying check whether the href contains 'cid'... 
     alert('your href contains cid'); 
    } else { 
     alert(' do nothing'); 
    }; 
    }); 
}); 

回答

3

使用indexOf它的方法:

if($(this).attr('href').indexOf('cid') != -1) 

你不需要.each() BTW:

$ul.find('a').click(function(e) { 
    //... 
}); 
+0

太感謝你了,這真是棒極了:) – SoulieBaby 2012-04-26 22:24:58