简体   繁体   中英

How to get img src with JSDOM (Node.js)

I need to find the src of the img tag in my string:

const dom = new JSDOM('<img src="godaddy.com">',
                      { includeNodeLocations: true });
console.log(dom.window.document.querySelector("img"));

But the console output is:

HTMLImageElement {}

How can I get the value of the src attribute?

I've not used the jsdom myself, but assuming it pretty much has same api's as browser dom. You could be able to use this:

const dom = new JSDOM('<img src="godaddy.com">', { includeNodeLocations: true });
console.log(dom.window.document.querySelector("img").getAttribute('src');

If you have multiple images then you can use querySelectorAll() which returns a NodeList object representing the set of matched elements. You can then loop through this list and get the src values.

More about querySelectorAll()

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