Jquery 如果包含某些模式,如何检查id名称?
我使用的是jquery,循环方式如下:Jquery 如果包含某些模式,如何检查id名称?,jquery,Jquery,我使用的是jquery,循环方式如下: $("span").each(function (index) { var idname = $(this).attr('id'); $("#" + idname).click(function () { window.location.href = "http://" + $(this).attr('id') + "lin.gw"; }); }); //end for click attachment to bu
$("span").each(function (index) {
var idname = $(this).attr('id');
$("#" + idname).click(function () {
window.location.href = "http://" + $(this).attr('id') + "lin.gw";
});
}); //end for click attachment to button
我想在
id
包含*raid*
的元素上循环。它的语法是什么?通过“like“raid”,我猜您的意思是它包含“raid”。最简单的方法是测试正则表达式:/raid/.test(idname)
。如果与“raid”类似,则返回true
;如果与“raid”不同,则返回false
。您可以使用正则表达式进行此操作。下面是regex邮件匹配的示例。您可以编写自己的正则表达式并检查“idname”是否包含“raid”
功能IsValidEmail(电子邮件){
var filter=/^([\w-.]+)@([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.|([\w-]+))([a-zA-Z]{2,4}.[0-9]{1,3})(]?)$/;
返回过滤器测试(电子邮件);
}
使用:
最好的方法是使用删除不需要的元素:
$("span").filter(function() {
if (this.id.toLowerCase().indexOf('raid') !== -1) {
return true;
} else {
return false;
}
}).click(function(){
window.location.href="http://"+this.id+"lin.gw";
});
它使用This.id
(远比$(This.attr('id')
)更有效)并使用一个单击调用而不是使用每个调用,这样更清晰,性能略有提高
$("span").filter(function() {
if (this.id.toLowerCase().indexOf('raid') !== -1) {
return true;
} else {
return false;
}
}).click(function(){
window.location.href="http://"+this.id+"lin.gw";
});