JavaScript/jQuery—从元素';身份证
来自以下标记JavaScript/jQuery—从元素';身份证,javascript,jquery,regex,Javascript,Jquery,Regex,来自以下标记 <div id="my-div"> <a href="#" id="link-1">Somewhere</a> <a href="#" id="link-2">Somewhere else</a> </div> $(this).attr('id').replace('link-','')只要前面的文本始终保持不变,您就可以使用该方法获取数字 $(this).attr('id').substri
<div id="my-div">
<a href="#" id="link-1">Somewhere</a>
<a href="#" id="link-2">Somewhere else</a>
</div>
$(this).attr('id').replace('link-','')
只要前面的文本始终保持不变,您就可以使用该方法获取数字
$(this).attr('id').substring(5)
使用正则表达式是您的最佳选择,例如:
// id contains '1' for id="link-1"
var id = parseInt(this.id.replace(/[^\d]/g, ''), 10);
如果您知道所有id的前缀都是“link-”,则只需获取id的子字符串:
$("#my-div a").click(function(){
alert(this.id.substr(5));
});
我通常会这样做:
$("#my-div a").click(function(){
var match;
if (match = $(this).attr('id').match(/link-(\d+)/)) {
var number = parseInt(match[1],10);
alert(number);
}
});
您可以使用正则表达式解析出数字:
var match = /link-(\d+)/.exec($(this).attr('id'));
var num = match[1];
你可以试试:
var n = $(this).attr('id').match(/link-(\d+)/)[1];
这将获取id
属性,与模式link-(\d+)
(意思是link-
,后跟一个或多个数字)匹配,然后提取第一个子表达式匹配项(括号中的部分\d+
),这应该是您要查找的数字
如果需要将n
作为整数而不是字符串使用,则应使用,确保指定以10为基数:
var n = parseInt($(this).attr('id').match(/link-(\d+)/)[1], 10);
如果您的id
属性不能保证以link-
开头,后跟一个或多个数字,并且您希望捕获这种情况而不是抛出错误,则应检查匹配的返回值
:
var match = $(this).attr('id').match(/link-(\d+)/);
if (match) {
var n = parseInt(match[1], 10);
alert(n);
} else {
// do something else if they don't match
}
这应该是最简单的方法:
var id = this.id.replace(/[^\d]/g,'')*1;
它将ID属性中的任何数字作为数字返回(*1
进行转换,类似于parseInt
)。在您的示例中:
$("#my-div a").click(function(){
var n = this.id.replace(/[^\d]/g,'')*1;
alert(n); // alerts any number in the ID attribute
alert(typeof n) // alerts 'number' (not 'string')
});
他正在询问如何在获得id后解析该id,这不是返回“link-1”吗?这将返回id。不仅返回请求的id的数字部分。$(this).attr('id')
,而不是$(this).id
收到,谢谢。在“ol复制粘贴”上稍微快一点。实际上,我只需要使用this.id
,您应该使用parseInt(…,10)代码>如果ID以0开头以停止八进制解析:parseInt('010')
=8。。。另外-在jQuery中,$(this).id
将是未定义的-请尝试$(this.attr('id')
,或者,实际上,只需this.id
。无需将jQuery拖到所有内容中。@gnarf@bobince-感谢您的评论,我已经更新了答案以反映它们。当然,修复为更简洁。为什么要在更简单的substr()
上使用此选项?如果您假设前五个字符是链接-
,那么您最好假设它的最后一部分是您要查找的数字,不是吗?@zombat-真的,但在将其视为一个链接之前,我宁愿断言我确实在使用“link-(\d+)”。@zombat因为它使您的代码更加自我记录。对于稍后阅读您的代码的人,他们可能不知道您为什么要进行substr
匹配,而正则表达式正好显示了您要查找的内容。它还意味着,如果您以某种方式将某个元素与另一种格式的id
进行匹配,那么它将失败;通常,提前失败并在源代码处捕获错误要比处理损坏的数据并在将该数据用于其他用途后再失败要好。(tl;格纳夫博士说的话)@Brian-我不同意。如果我使用这行代码var n=parseInt($(this).attr('id').match(/link-(\d+)/)[1],10)
对于不以链接-
开头的元素,它会产生一个错误,并在该函数中结束对Javascript的处理。最好继续使用空匹配(例如意外id上的substr()
),并在使用它之前检查匹配是否确实是intsubstr()
也比正则表达式简单得多,并且提高了代码的可读性。我认为&&matched.length>1
是不必要的,因为您使用的是/(\d+/
如果不捕获某些内容,regexp就不可能匹配。+1用于使用regex'()'分组。您也不需要“link-”,只要.match(/(\d+/)就可以了。谁在乎引导文本是什么?@Jarrett-我宁愿确保它是一个链接-?
,而不是仅仅说“任何旧的数字都会有用”。。。这样,如果你有一个
,它将不会使用链接-
特定的代码。.id
不起作用,我认为。您需要首先列出的attr('id')
表单。正如gnarf指出的,$(this).id
不起作用。如果在这个
周围使用jQuery对象,那么必须使用attr()
。另外,我认为您的意思是子字符串(5)
,而不是子字符串(6)
。干杯。我看到其他人使用.id,并认为这是我从jQuery中漏掉的东西。谢谢僵尸,哈哈。我真不敢相信这么长时间才得到一个基于分裂的答案+1.
var match = $(this).attr('id').match(/link-(\d+)/);
if (match) {
var n = parseInt(match[1], 10);
alert(n);
} else {
// do something else if they don't match
}
var id = this.id.replace(/[^\d]/g,'')*1;
$("#my-div a").click(function(){
var n = this.id.replace(/[^\d]/g,'')*1;
alert(n); // alerts any number in the ID attribute
alert(typeof n) // alerts 'number' (not 'string')
});