简体   繁体   中英

Blogspot post content : How to get second image src using javascript

I'm working on a blogspot template and I need to get the second image from post content

I've tried this code but I didn't get any results:

var postcontent = entry.content.$t; // The Post Content 
var images = postcontent.getElementsByTagName('img'); 
 document.write(images[1]);

I hope this fits your use case, also if this dosent work log the item object to see how you can access the data

var postcontent = entry.content.$t; // The Post Content 
var images = postcontent.getElementsByTagName('img'); 
images.forEach(item=>{
 console.log("The item is",item)
 document.write(item.src)
 })

;

entry.content.$t contains a javascript string.

So you need to append the string to an element before using getElementsByTagName

var postcontent = entry.content.$t; // The Post Content 
var elem = document.createElement('div');
elem.innerHTML = postcontent;
var images = elem.getElementsByTagName('img');
document.write(images[1]);

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