如何通过jquery获取匹配(id) 我想获取(id)在(href)之间(span)
如果代码 这里
HTML
<table>
<tr>
<td class='zero'>
<span>
<a href="http//google&id=222.com">123</a>
<a>hgi</a>
</span>
<span>
<a href="http//google&id=111.com">123</a>
<a>hgi</a>
</span>
<span>
<a href="http//google&id=333.com">123</a>
<a>hgi</a>
</span>
</td>
</tr>
</table>
jquery
$(".zero span").each(function(){
$(this).find("a").attr("href").match(/id=/);
});
但是错误>_< 为什么 ? 和 怎么办?
我想要
id=222
id= 333
id= 111
最佳答案
您的一些<a />
元素没有 href
属性(property)。 attr()
将返回undefined
在这种情况下。当尝试调用match()
时上undefined
,JavaScript 抛出错误。
修复它:
$(".zero span").each(function(){
href = $(this).find("a").attr("href");
if (href !== undefined) {
href.match(/id=/);
}
});
或者更改选择器以包含 a[href]
.
外部:
$(".zero span a[href]").each(function(){
$(this).attr("href").match(/id=/);
});
内部:
$(".zero span").each(function(){
$(this).find("a[href]").attr("href").match(/id=/);
});
关于javascript - jquery如何获取比赛id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25593446/