python中循環語句有兩種,while,for;python
JavaScript中循環語句有四種,while,do/while,for,for/inide
jQuery循環語句each函數
2.1 for 循環this
# a、 li = [1, 2, 3, 4] for i in li: print(i)
# b、 li = [1, 2, 3, 4] for i, j in enumerate(li, 1): print(i, j) # enumerate(li, 1)中的 1 表明索引從 1 開始,默認爲空,表明從 0 開始
# c、 li1 = [1, 3, 5, 7] li2 = [2, 4, 6, 8] for i, j in zip(li1, li2): print(i, j)
# d、不要經過 dic.items()來循環字典,效率會很是低 dic = {'a': 1, 'b': 2} for k in dic: print(k, dic.get(k)) # 上述代碼中,至關於對字典的key進行循環,等價於下面的代碼: dic={'a': 1, 'b': 2} for k in dic.keys(): print(k, dic.get(k)) # 對於值的循環,即 for v in dic.values() ...
while True: pass # 在python中除了none、''、[]、{}、()、False,其餘均爲真值,即爲True。 # 對於循環判斷:eg. flag # 判斷是否爲真:while flag: # 判斷是否爲假:while not flag:
var count = 0; while(count < 10){ console.log(count); count ++; } # JavaScript定義局部變量用var
do{ 代碼塊; }while(條件語句)
var a = document.getElementById('key').children; for(var i=0; i<a.length; i++){ var inp=a[i]; var at=inp.getAttribute('type'); if(at=='text'){ inp.setAttribute('value', '123'); } } # 獲取id='key'下全部type='text'的標籤並設置value值等於'123'.
var c1 = document.getElementById('i1').getElementsByTagName('input'); for(var i in c1){ if(c1[i].checked){ c1[i].checked=false; }else{ c1[i].checked=true; } }
each語句:spa
$(':text').each(function(){ console.log($(this).val()) ; }); # $(':text') ==> $('input[type="text"]')
語法規則:標籤集合.each(匿名函數)orm
上述代碼的意思是:獲取全部inp標籤中type='text',的標籤,並對其進行循環,每次打印它的值。索引
jQuery中跳出循環用return:ip
return true:退出本次循環,執行下次循環,至關於其它語言的continue;get
return false:退出本層循環,即退出當前each,至關於其它語言的break;input