Web開發(初級)- python、JavaScript及jQuery迴圈語句

來源:互聯網
上載者:User

標籤:python   開發   

迴圈語句一、概述

    python中迴圈語句有兩種,while,for;

    JavaScript中迴圈語句有四種,while,do/while,for,for/in

    jQuery迴圈語句each

二、python迴圈語句


2.1 for 迴圈

# 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() ...

2.2 while迴圈
while True:    pass    # 在python中除了none、‘‘、[]、{}、()、False,其他均為真值,即為True。# 對於迴圈判斷:eg. flag# 判斷是否為真:while flag:# 判斷是否為假:while not flag:
三、JavaScript迴圈語句a、while迴圈
var count = 0;while(count < 10){    console.log(count);    count ++;}# JavaScript定義局部變數用var
b、do/while
do{   代碼塊;}while(條件陳述式)
c、for
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‘.
d、for  in
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;  }}
四、jQuery迴圈語句

each語句:

$(‘:text‘).each(function(){    console.log($(this).val()) ;});# $(‘:text‘) ==> $(‘input[type="text"]‘)

文法規則:標籤集合.each(匿名函數)

    上述代碼的意思是:擷取所有inp標籤中type=‘text‘,的標籤,並對其進行迴圈,每次列印它的值。

    jQuery中跳出迴圈用return:

        return true:退出本次迴圈,執行下次迴圈,相當於其它語言的continue;

        return false:退出本層迴圈,即退出當前each,相當於其它語言的break;





Web開發(初級)- python、JavaScript及jQuery迴圈語句

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.