簡體   English   中英

jQuery:如何突破each()及其內部的功能?

[英]Jquery: How to break out of each() and the function it's inside in?

$("#savebtn").click(function() {

    $('.mytable tr').each(function(){
      if($('.mytable tr').length < 1){
          alert("Nothing in the table!")
          return 
      }
    });
alert("You are still inside the click function")
});

對此有一點麻煩。 我想退出jquery每個循環並完全單擊功能。 但是“返回”只是讓我退出了每個循環。 如何完全退出點擊功能?

如果表中沒有任何內容,則each函數實際上將永遠不會執行 (因為要迭代的集合為空)。

也許您打算將條件作為click功能的第一行:

$("#savebtn").click(function() {
    if($('.mytable tr').length < 1) {
        alert("Nothing in the table!");
        return;
    }

    $('.mytable tr').each(function() {
        doSomething(this);
    });

    alert("You are still inside the click function")
});

附帶說明一下,如果each功能都是該塊中的唯一內容,則無需為空表情況做任何特殊的事情,因為每個功能將簡單地執行零次。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM