在jQuery上创建带有标记的数组
我想从body中获取所有的“p”并创建一个数组,然后使用此数组打印每个“p”的文本内容,如何存档?这是一个非常初级的问题,但我被卡住了 谢谢。在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"
.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() );
});