So i want to choose what XML data to display (without using attributes) using Javascript. Here are the specifics about what i want to do:
XML
<channel>
<item>
<title>3.jpg</title>
<link>images/clean/3-dirty.jpg</link>
</item>
<item>
<title>4.jpg</title>
<link>images/clean/4-dirty.jpg</link>
</item>
<item>
<title>5.jpg</title>
<link>images/clean/4-clean.jpg</link>
</item>
<item>
<title>6.jpg</title>
<link>images/clean/4-dirty.jpg</link>
</item>
... and the list goes on for ages.
</channel>
Javascript
<script type="text/javascript">
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","xml/drc.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
var x=xmlDoc.getElementsByTagName("item");
for (i=0;i<x.length;i++)
{
document.write("<img src='");
document.write(x[i].getElementsByTagName("link")[0].childNodes[0].nodeValue);
document.write("' alt='image' height='220px' width='260px' />");
}
</script>
How can i display images that only contains the word "dirty" or "clean" in the url? I've tried using IndexOf() method but can't get it to work.
try this:
for (i=0;i<x.length;i++) {
var src = x[i].getElementsByTagName("link")[0].childNodes[0].nodeValue;
if( src.indexOf("dirty") != -1 ) {
document.write("<img src='");
document.write(src);
document.write("' alt='image' height='220px' width='260px' />");
}
}
if it doesn't work, try this, too:
for (i=0;i<x.length;i++) {
var src = x[i].getElementsByTagName("link")[0].childNodes[0].nodeValue;
var srcLower = src.toLowerCase();
if( srcLower.indexOf("dirty") != -1 ) {
document.write("<img src='");
document.write(src);
document.write("' alt='image' height='220px' width='260px' />");
}
}
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.