Cheerio js를 사용하여 웹 사이트를 고치고 있습니다. $ .html ('text')를 사용하여 내부 HTML의 내용을 수정할 때 배열에 표 줄 목록이 있습니다. for 루프가 작동하는 것 같다하지만 함수가 종료되면 나는 수정 된 텍스트 잃게 : 당신은 아마 원래의 소스 데이터를 수정하지 않습니다 안녕로 bands.push($.html())
을 원하는Cheerio js가 내부 HTML을 수정할 수 없습니다.
var cheerio = require('cheerio');
var bands = [];
var res = function (data) {
for (var j=0; j < data.length; j++) {
var perline = data[j];
var $ = cheerio.load(perline);
var chline = $('[class^="eventSlot"]');
for (var i=0; i < chline.length; i++) {
console.log($(chline[i]).html()); // looks correct
$(chline[i]).html('some text'); // modify inner HTML
console.log($(chline[i]).html()); // looks modified
}
bands.push(perline);
}
return bands;
};
var html = ['<td>11/04/2014</td><td><span class="eventSlot slot1 headliner">Band1</span><span class="eventSlot slot2">Band2</span></td><td>',
'<td>11/04/2014</td><td><span class="eventSlot slot1 headliner">Band3</span></td>'];
console.log(res(html)); // contents are not modified from original html
이것을 알아 냈습니까? 그렇지 않다면, 지금 일하고 있어요. 알아 내면 다시 말하겠습니다. Cheerio를 사용하여'$ .html()'로 html 문자열을 export 한 후에'replaceWith()'를 사용한다고 말하고 싶지만 확실하지 않습니다. –