没有儿童选择
我有一些 html,其中一些标签没有任何子标签,我想识别它们并计算这些标签的数量。
我已尝试以下方法,但似乎不起作用:
var count = 0;
$("th[class=gridlabel]").each(function(){
if($("th[class=gridlabel]").children().length<1)
{
count++;
}
});
return count;
alert(count);
不确定这里出了什么问题。
我从 html 源代码中添加了一些额外的脚本,我想确保“count”只会计算前两个标签,即“answer 3”和“answer 4”,因为它们的标签没有子标签:
<tr>
<th class="gridlabel">answer 3</th>
<td headers="q12_header1" class="gridcell"><input><div><span>answer 3</span><label>1</label></div></td>
<td headers="q12_header2" class="gridcell"><input><div><label></label></div></td>
</tr>
<tr>
<th class="gridlabel">answer 4</th>
<td headers="q12_header1" class="gridcell"><input><div><span>answer 4</span><label>1</label></div></td>
<td headers="q12_header2" class="gridcell"><input><div><label></label></div></td>
</tr>
<tr>
<th class="gridlabel">other specify 1<input><label>other specify 1</label></th>
<td><input><div><span>other specify 1</span><label>1</label></div></td>
<td><input><div><label>2</label></div></td>
</tr>
I have some htmls, where some tags does not have any children, and I want to identify them and count the number of those tags.
I have tried the following and it doesn't seem to work:
var count = 0;
$("th[class=gridlabel]").each(function(){
if($("th[class=gridlabel]").children().length<1)
{
count++;
}
});
return count;
alert(count);
not sure what's the issue here.
I have added some additional scripts from the html source code, I want to ensure the "count" will only count the first two tags, for "answer 3" and "answer 4", as their tags do not have children:
<tr>
<th class="gridlabel">answer 3</th>
<td headers="q12_header1" class="gridcell"><input><div><span>answer 3</span><label>1</label></div></td>
<td headers="q12_header2" class="gridcell"><input><div><label></label></div></td>
</tr>
<tr>
<th class="gridlabel">answer 4</th>
<td headers="q12_header1" class="gridcell"><input><div><span>answer 4</span><label>1</label></div></td>
<td headers="q12_header2" class="gridcell"><input><div><label></label></div></td>
</tr>
<tr>
<th class="gridlabel">other specify 1<input><label>other specify 1</label></th>
<td><input><div><span>other specify 1</span><label>1</label></div></td>
<td><input><div><label>2</label></div></td>
</tr>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更新
鉴于您提供的 HTML,如果您知道“非空”单元格将有一个输入子项,则可以使用类似的内容:
如果您想要更灵活一点但更长一点的内容,你可以这样做:
这样你就可以覆盖任何可能的子标签。这实际上取决于您的需要。
Update
Given the HTML you provided, you could use something like this if you know that "non-empty" cells will have an input child :
If you want something a little more flexible, but a little longer, you can do something like :
That way you'll cover any possible child tag. It really depends on what you need.
试试这个:
Try this: