How to put all elements' content in array using jQuery ?

后端 未结 2 1275
时光说笑
时光说笑 2020-12-04 00:34

Text1

Text2

Text3

Result should be :

[         


        
2条回答
  •  感情败类
    2020-12-04 00:56

    This will work:

    var p = $('#main p').map(function () {
            return '"' + $(this).text() + '"';
        }).get().join(',');
        p = "[" + p + "]";
    

    map() lets you iterate over each match and get a value from it, which is inserted into an array-like object. get() then returns it as a Javascript array, and .join makes the array into a string.

提交回复
热议问题