【发布时间】:2016-09-04 04:43:29
【问题描述】:
我正在尝试使用 each 函数,但我需要匹配确切的属性名称
jquery
$('[data*="name"]').each(function(i, obj){dostuff}
只有在找到 name 时才需要 {dostuff} 而不是在找到 names 时。有什么建议吗?
【问题讨论】:
标签: javascript jquery
我正在尝试使用 each 函数,但我需要匹配确切的属性名称
jquery
$('[data*="name"]').each(function(i, obj){dostuff}
只有在找到 name 时才需要 {dostuff} 而不是在找到 names 时。有什么建议吗?
【问题讨论】:
标签: javascript jquery
【讨论】:
我认为你应该这样做。
$('[data="name"]').each(function(i, obj){ dostuff });
希望对你有所帮助! :)
【讨论】:
您应该可以只使用attribute equals selector,它与您当前的选择器基本相同,没有*(因为*= 用作"contains" selector):
// This would perform your operation to every element that had a data attribute of
// "name" (i.e. <span data="name">, etc.)
$('[data="name"]').each(function(i, obj){
dostuff();
});
【讨论】:
只是
$('[data="name"]').each(function(i, obj){dostuff}
或
$('[data*="name"]:not([data*=names])').each(function(i, obj){dostuff}
【讨论】:
从属性选择器(* 字符)中删除“包含”条件
$('[data="name"]').each(function(i, obj){dostuff}
在属性名后使用 *
选择具有指定属性和值的元素 包含给定的子字符串
阅读here
【讨论】: