Javascript 是否可以只在for循环内部运行一次代码块?

Javascript 是否可以只在for循环内部运行一次代码块?,javascript,jquery,Javascript,Jquery,我使用循环查找包含特定类单元格的整个表列,它适用于应用类和下面的其他内容。唯一的问题是,我还想输出一次单元格的值。这有可能吗 $('td:first-child').each(function() { for (var i = 0; i <= 5; i++) { var col = $('.tabell tr').find('td:nth-child(' + i + ').check').length; if (col == 5) {

我使用循环查找包含特定类单元格的整个表列,它适用于应用类和下面的其他内容。唯一的问题是,我还想输出一次单元格的值。这有可能吗

$('td:first-child').each(function() {
    for (var i = 0; i <= 5; i++) {
        var col = $('.tabell tr').find('td:nth-child(' + i + ').check').length;  
        if (col == 5) {
            $(".bingocl").fadeIn(2000);
            var column = $('.tabell tr').find('td:nth-child(' + i + ')');
            column.addClass("bingo", 2000);  
            var text = column.text().toUpperCase(); 
            $("#textout").append(text + "!!");         
        }
    }
});
$('td:first child')。每个(函数(){

对于(var i=0;i您可以执行
column.html();
来获取单元格内容

您可以尝试使用
break
。 链接:

尝试使用中断:

$('td:first-child').each(function() {
for (var i = 0; i <= 5; i++) {
    var col = $('.tabell tr').find('td:nth-child(' + i + ').check').length;  
    if (col == 5) {
        $(".bingocl").fadeIn(2000);
        var column = $('.tabell tr').find('td:nth-child(' + i + ')');
        column.addClass("bingo", 2000);  
        var text = column.text().toUpperCase(); 
        $("#textout").append(text + "!!");   
        break;      
    }
}
});
$('td:first child')。每个(函数(){

对于(var i=0;i假设您已经有权访问获胜的行/列(您向它们添加
bingo
类),您可以访问每个元素以输出其值

您的代码变成:

var main = function() {

  //Styling the rows 
  $(".tabell tbody").find("tr").each(function(idx) {
    var row = $(this);
    if (row.find("td").length == row.find("td.check").length) {
      row.addClass("bingo");
      $(".bingocl").fadeIn(2000);

      // Iterate your row elements
      row.each(function(){document.write($(this).html() + " ");});
    }
  });

  //styling cols
  //$('td:first-child').each(function() { <- remove this
    for (var i = 0; i <= 5; i++) {
      var col = $('.tabell tr').find('td:nth-child(' + i + ').check').length;
      if (col == 5) {
        $(".bingocl").fadeIn(2000);
        var column = $('.tabell tr').find('td:nth-child(' + i + ')');
        column.addClass("bingo", 2000);

        // Iterate your column elements
        column.each(function(){document.write($(this).html() + " ");});

        break;
      }
    }
  //}); <- remove this
}
$(document).ready(main);
。选定的\u列{
背景:蓝色;
}
.选定的行{
背景:黄色;
}
.所选列.所选行{
背景:绿色;
}
B.宾果{
边框:2倍固体石灰;
}

A.
B
C
D
E
F
G
H
我
J
K
L
M
N
O
P
Q
R
s
T
U
v
W
X
Y

#text输出:

我也想输出一次单元格的值。这可能吗?
你在哪里输出值?代码中不清楚是的,我意识到,现在补充说,不清楚你想要实现什么。你能提供一个小提琴吗?@PinkTurtle这里是在实时环境中,涉及很多PHPd所以不能使用fiddle。基本上是一个宾果游戏,当它包含某些单词时,我想使用jquery对行和列进行样式化,并且(这篇文章就是关于这个)输出单元格的值。我添加了一个答案,以防您尚未解决问题。我认为这就是您要寻找的。使用break,它根本没有输出任何文本。不幸的是,您也可以在中断循环之前输出任何文本,您还可以将其存储到任何全局变量aur json中,在输出文本后,您可能会中断ItEdit:它确实输出文本,但五次都没有中断;看起来真的很好,我唯一关心的是文档写入。由于文本与表(内置于PHP函数中)发布在同一页上,是否可以实现相同的效果,但指定文本显示在哪个div中?是的ofc。请参阅我的编辑。使用
$(“#textout”)。追加(…)
就像你在问题中所做的那样。我尝试了5次,但它得到了输出,因为它是在for循环中调用的。这就是所有这些的问题。你添加了
break
语句吗?是的,请参阅原始帖子中的更新。不幸的是,相同的结果。
$('td:first-child').each(function() {
for (var i = 0; i <= 5; i++) {
    var col = $('.tabell tr').find('td:nth-child(' + i + ').check').length;  
    if (col == 5) {
        $(".bingocl").fadeIn(2000);
        var column = $('.tabell tr').find('td:nth-child(' + i + ')');
        column.addClass("bingo", 2000);  
        var text = column.text().toUpperCase(); 
        $("#textout").append(text + "!!");   
        break;      
    }
}
});
var main = function() {

  //Styling the rows 
  $(".tabell tbody").find("tr").each(function(idx) {
    var row = $(this);
    if (row.find("td").length == row.find("td.check").length) {
      row.addClass("bingo");
      $(".bingocl").fadeIn(2000);

      // Iterate your row elements
      row.each(function(){document.write($(this).html() + " ");});
    }
  });

  //styling cols
  //$('td:first-child').each(function() { <- remove this
    for (var i = 0; i <= 5; i++) {
      var col = $('.tabell tr').find('td:nth-child(' + i + ').check').length;
      if (col == 5) {
        $(".bingocl").fadeIn(2000);
        var column = $('.tabell tr').find('td:nth-child(' + i + ')');
        column.addClass("bingo", 2000);

        // Iterate your column elements
        column.each(function(){document.write($(this).html() + " ");});

        break;
      }
    }
  //}); <- remove this
}
$(document).ready(main);