2012-04-26 96 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