【发布时间】:2013-12-29 23:59:39
【问题描述】:
我正在使用 jQuery 选择器,我需要根据字符的位置选择一些元素。例如,我需要选择那些第 3 个字符为“h”,第 4 个字符为“r”的项目。从下面的列表中,选择器应该选择前两个元素,而不是第三个。
abhres
cdhrex
efdghr
【问题讨论】:
标签: jquery html css regex jquery-selectors
我正在使用 jQuery 选择器,我需要根据字符的位置选择一些元素。例如,我需要选择那些第 3 个字符为“h”,第 4 个字符为“r”的项目。从下面的列表中,选择器应该选择前两个元素,而不是第三个。
abhres
cdhrex
efdghr
【问题讨论】:
标签: jquery html css regex jquery-selectors
您将不得不编写一个自定义选择器http://jquery-howto.blogspot.com/2009/06/jquery-custom-selectors-with-parameters.html Bellow 是我认为可以满足您的需求的一个示例。如果不是,那么修改它以获得您想要的东西应该不难
$.expr[':']['nth-letter-eq'] = function (obj, index, meta, stack) {{
var args = meta[3].split(",");
var includeThisElement = false;
if ($(obj).text().charAt(parseInt(args[0])) == args[1]) {
includeThisElement = true;
}
return includeThisElement;
}
用法
$("p:nth-letter-eq(3,a)")
【讨论】:
【讨论】: