선택자 "cheerio"와 같은이 jQuery는 html 페이지의 주석 노드에서 주석을 가져 오려고합니다.
$는 cheerio 개체입니다. 어떻게 할 수 있습니까? 감사
console.log($('*').contents().length); //reports back more than 1000
$('*').contents().filter(function() {
if (this.nodeType == 8) {
//the following gives null for every node found
console.log($(this).html());
//the following gives blank for every node found
console.log($(this).text());
}
});
댓글의 내용은 HTML ( .innerHTML
) 또는 값 ( .value
) 이 아니며 .nodeValue
. jQuery는이를 얻을 수있는 기능을 제공하지 않으며 Cheerio도 기능을 제공하지 않지만 필요하지는 않습니다. 다음을 사용하십시오 this.nodeValue
.
$('*').contents().filter(function() {
if (this.nodeType == 8) {
console.log(this.nodeValue);
}
});
(내가 사용했던 filter
당신의 예했기 때문에 거기에,하지만 당신은 반환 값을 사용하지 않는 경우 filter
, each
더 의미가 있습니다.)
다음은 DOM 예제이지만 아마도 Cheerio는 비슷하게 작동 할 것입니다.
$("*").contents().each(function() {
if (this.nodeType === 8) {
console.log(this.nodeValue);
}
});
<!-- Comment 1 -->
<!-- Comment 2 -->
<!-- Comment 3 -->
<!-- Comment 4 -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다