Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在jQuery上创建带有标记的数组_Jquery_Arrays_Tags - Fatal编程技术网

在jQuery上创建带有标记的数组

在jQuery上创建带有标记的数组,jquery,arrays,tags,Jquery,Arrays,Tags,我想从body中获取所有的“p”并创建一个数组,然后使用此数组打印每个“p”的文本内容,如何存档?这是一个非常初级的问题,但我被卡住了 谢谢。在jquery中尝试使用.map() var result = $("body").find("p").map(function() { return $(this).text(); }).get(); console.log(result); 在jquery中尝试使用.map() var result = $("body").find("p"

我想从body中获取所有的“p”并创建一个数组,然后使用此数组打印每个“p”的文本内容,如何存档?这是一个非常初级的问题,但我被卡住了

谢谢。

在jquery中尝试使用
.map()

var result = $("body").find("p").map(function() {
   return $(this).text();
}).get();

 console.log(result);

在jquery中尝试使用
.map()

var result = $("body").find("p").map(function() {
   return $(this).text();
}).get();

 console.log(result);

在jquery中尝试使用
.map()

var result = $("body").find("p").map(function() {
   return $(this).text();
}).get();

 console.log(result);

在jquery中尝试使用
.map()

var result = $("body").find("p").map(function() {
   return $(this).text();
}).get();

 console.log(result);

无论是使用纯JavaScript还是jQuery,都有很多方法可以做到这一点:

JavaScript:

var p = document.getElementsByTagName('p'); // Get all paragraphs (it's fastest way by using getElementsByTagName)
var pTexts = []; // Define an array which will contain text from paragraphs


// Loop through paragraphs array

for (var i = 0, l = p.length; i < l; i++) {
    pTexts.push(p[i].textContent); // add text to the array
}
$.each(p, function(i, el) {
    pTexts.push(el.textContent);

    // or using jQuery text(), pretty much the same as above
    // pTexts.push( $(el).text() );
});

无论是使用纯JavaScript还是jQuery,都有很多方法可以做到这一点:

JavaScript:

var p = document.getElementsByTagName('p'); // Get all paragraphs (it's fastest way by using getElementsByTagName)
var pTexts = []; // Define an array which will contain text from paragraphs


// Loop through paragraphs array

for (var i = 0, l = p.length; i < l; i++) {
    pTexts.push(p[i].textContent); // add text to the array
}
$.each(p, function(i, el) {
    pTexts.push(el.textContent);

    // or using jQuery text(), pretty much the same as above
    // pTexts.push( $(el).text() );
});

无论是使用纯JavaScript还是jQuery,都有很多方法可以做到这一点:

JavaScript:

var p = document.getElementsByTagName('p'); // Get all paragraphs (it's fastest way by using getElementsByTagName)
var pTexts = []; // Define an array which will contain text from paragraphs


// Loop through paragraphs array

for (var i = 0, l = p.length; i < l; i++) {
    pTexts.push(p[i].textContent); // add text to the array
}
$.each(p, function(i, el) {
    pTexts.push(el.textContent);

    // or using jQuery text(), pretty much the same as above
    // pTexts.push( $(el).text() );
});

无论是使用纯JavaScript还是jQuery,都有很多方法可以做到这一点:

JavaScript:

var p = document.getElementsByTagName('p'); // Get all paragraphs (it's fastest way by using getElementsByTagName)
var pTexts = []; // Define an array which will contain text from paragraphs


// Loop through paragraphs array

for (var i = 0, l = p.length; i < l; i++) {
    pTexts.push(p[i].textContent); // add text to the array
}
$.each(p, function(i, el) {
    pTexts.push(el.textContent);

    // or using jQuery text(), pretty much the same as above
    // pTexts.push( $(el).text() );
});