Percobaan kali ini adalah cara mendapatkan gambar pertama pada halaman HTML untuk ditampilkan pada element tag html lainnya :
contoh kode javascript :
<div id="result"></div>
var post_body = "<div>This is an image <img src='abcd.jpg' /> and this is a paragraph <p>hi there</p> Here we have another image <img src='pqrs.jpg' /></div>"
var div = document.createElement('div');
div.innerHTML = post_body;
var firstImage = div.getElementsByTagName('img')[0]
var imgSrc = firstImage ? firstImage.src : "";
// or, if you want the unresolved src, as it appears in the original HTML:
var rawImgSrc = firstImage ? firstImage.getAttribute("src") : "";
$("#result").append('<img src="'+imgSrc+'" style="width:150px"/>');
Smoga dapat difahami.....
ConversionConversion EmoticonEmoticon