2016-11-21 2 views
0

Я только хочу получить доступ к тексту h1 (H1 title is here в этом случае), но он печатает все. Я попытался добавить .remove('.small-title') до text(), но это не сработало.Доступ к тексту класса, который содержит другие элементы, используя Cheerio

<div class="modal-know> 
    <h1> 
    H1 title is here 
    <div class="small-title"> 
    <a href="title-a">Click</a> 
    <a href="title-b">Click 2</a> 
    </div> 
    </h1> 
</div> 

Node.js код

var newsTitle = $2('.modal-know h1').text(); // prints out everything 
console.log(newsTitle); 

ответ

0

имеют вид на cheerio docs: text()

это говорит

включая их потомков

Это такое же поведение, что JQuery .text()

Так что, возможно, этот ответ может помочь вам: jQuery: using .text() to retrieve only text not nested in child tags

Здесь у вас есть код я тестировал:

let newsTitle = $('.modal-know h1').contents()[0].nodeValue; 
// solution 2: 
// .clone() //clone the element 
// .children() //select all the children 
// .remove() //remove all the children 
// .end() //again go back to selected element 
// .text(); // prints out everything 

//solution 3: 
// .contents().filter(function(){ 
//  return this.nodeType == 3; 
// })[0].nodeValue; 
console.log(newsTitle); 

* в вашем образце кода Ther «отсутствует» в классе «модальный мода»

<div class="modal-know> -> <div class="modal-know"> 
Смежные вопросы