我有几行具有这些 ID:
<tr id='0'></tr>
<tr id='0-0'></tr>
<tr id='0-0-0'></tr>
<tr id='0-1'></tr>
现在,鉴于我只知道第一个字符,是否可以只返回带有 1 个破折号的行?我的意思是 0-0、0-1,但不是 0-0-0。我知道有一个 css 选择器查询 ID 以(下面)开头的元素,但它返回我不想返回的元素。请问有没有简单的查询方法?
jQuery('[id^="0-"]')
谢谢
最佳答案
$(document).ready(function() {
$('tr').each(function() {
str = $(this).attr('id').split('-');
if (str.length == 2) {
alert($(this).attr('id'));
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr id='0'></tr>
<tr id='0-0'></tr>
<tr id='0-0-0'></tr>
<tr id='0-1'></tr>
</table>
关于javascript - 基于字符出现的 CSS 选择器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31617841/