没有儿童选择

发布于 2024-12-17 16:19:34 字数 1497 浏览 2 评论 0原文

我有一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青朷 2024-12-24 16:19:34
var count = $("th.gridlabel:empty").length;
return count;

更新

鉴于您提供的 HTML,如果您知道“非空”单元格将有一个输入子项,则可以使用类似的内容:

var count = $('th:not(:has(input))').length;

如果您想要更灵活一点但更长一点的内容,你可以这样做:

var count = 0;
$('th').each(function(){
    if($(this).children().length < 1)
    {
        count++;
    }
})

这样你就可以覆盖任何可能的子标签。这实际上取决于您的需要。

var count = $("th.gridlabel:empty").length;
return count;

Update

Given the HTML you provided, you could use something like this if you know that "non-empty" cells will have an input child :

var count = $('th:not(:has(input))').length;

If you want something a little more flexible, but a little longer, you can do something like :

var count = 0;
$('th').each(function(){
    if($(this).children().length < 1)
    {
        count++;
    }
})

That way you'll cover any possible child tag. It really depends on what you need.

夜司空 2024-12-24 16:19:34

试试这个:

var count = 0;
$("th[class=gridlabel]").each(function(){
    if(this.children().length<1)
    {
        count++;
    }
});
return count;
alert(count);

Try this:

var count = 0;
$("th[class=gridlabel]").each(function(){
    if(this.children().length<1)
    {
        count++;
    }
});
return count;
alert(count);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文