jQuery map和each用法

<!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>

 

posted @ 2017-02-20 16:22  流火行者  阅读(288)  评论(0编辑  收藏  举报