简体   繁体   中英

How do I store the content returned by text() in jquery?

I have been trying to extract the full-text content from the HTML document for computation and I was able to find the solution for that in jquery but it's quite partial... The output is as expected for the following code:

$(document).ready(function(){ 
    console.log($("*").text())
})

This is the output I was talking about. I want to store the content in the console in a variable. When I tried doing something like

var words = []
$(document).ready(function(){ 
    words.push($("*").text())
})
console.log(words)

it returns undefined . I came to know that it is because of the async of the callback. How do I approach this issue. Thanks in advance.

Consider your Selector, I think your scope is grabbing too many elements. Look at the following.

 $(function() { var words = []; $("body").children().not("script").each(function(i, el) { words.push($(el).text().trim()); }); console.log(words); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <h1>Introduction</h1> <p>This is a paragraph. </p> <div class="footer">02.12.2020</div>

This will iterate all the child elements of the Body tag. It will read the Text of each element and enter it into it's own entry in the array. Your result would be like:

[
  "Introduction",
  "This is a paragraph.",
  "02.12.2020"
]

One way to do this would be to get all the elements in the body, iterate over them to get their text content. With jQuery it would look something like this:

 $(document).ready(function() { let content = [] $('body * :not(script)').each((i, el) => { content.push($(el).text()) }) console.log(content) })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <aside> <h1>JS Documentation</h1> <ul> <li>Introduction</li> <li>What you should already know</li> </ul> </aside> <main> <h2>Introduction</h2> <p>JavaScript is a cross platform...</p> </main>

Note : the :not(script) selector will leave out any <script> tags (if present) in the <body> of the document.

Tip : If you need to get rid of line breaks whitespace you can use something like this:

text().trim().replace(/\r?\n|\r/g, '')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM