jQuery map和each用法

流火行者發表於2017-02-20
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
</head>
<body>
<div>
    <ul class="ul0">
        <li>aaa</li>
        <li>bbb</li>
        <li>ccc</li>
        <li>ddd</li>
    </ul>
</div>
</body>
</html>
<script src="jzzqmobile/js/jquery.min.js"></script>
<script>
    /*輸出
     each:aaa
     each:bbb
     each:ccc
     each:ddd
    */
    $('.ul0 li').each(function(){
        console.log('each:'+$(this).text())
    })
    /*輸出
     map:aaa
     map:bbb
     map:ccc
     map:ddd
     */
    $('.ul0 li').map(function(){
        console.log('map:'+$(this).text())
    })
    /*輸出
    1
    2
    3
    4
    */
    var arr=[{num:1},{num:2},{num:3},{num:4}]
    $.each(arr,function(){
        console.log(this.num+'###')
    })
    /*輸出
    Object {num: 1}
    Object {num: 2}
    Object {num: 3}
    Object {num: 4}
    */
    $.map(arr,function(item){
        console.log(item)
    })
</script>

 

相關文章