creating json from DOM elements using jQuery

前端 未结 3 1612
终归单人心
终归单人心 2021-01-03 03:39

If I have the following DOM elements

content1
content2

how, usi

3条回答
  •  清歌不尽
    2021-01-03 03:46

    http://jsfiddle.net/24JjD/

    var datas = [{ 
        'classname': 'item',
        'content': 'content1'
        }, {
        'classname': 'item',
        'content': 'content2'
        }
    ];
    
    $.each(datas, function(key, value) {
        $('body').append('
    '+value.content+'
    '); });​

    Correct answer :

    http://jsfiddle.net/tS9r5/

    var datas = [];
    
    $('div.item').each(function() {
       var data = { 
           classname: this.className, 
           content: $(this).text()
       };
       datas.push(data);
    });
    
    console.log(datas);
    

提交回复
热议问题