
Originally Posted by
Max Height
Yes, that can be done fairly easily. This is starting to sound a bit like some sort of homework exercise and so you should post at least your attempt and we can then help you get it working.
But essentially, all you need to do is:
1) wrap the <img> in an <a> in the html
2) add the url you want each clicked image to navigate to in the image's row in picData
3) add 1 line of code in showPic(num) to set the href of the <img>'s <a> to the url for that image
1 - I think I got that part right
2 - this one too
3 - Purely a guess on my part. I have no clue what to do here.
Here's what I've got so far:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title></title>
<style type="text/css">
#linksCont {
list-style-type: none;
}
#img1{
display: none;
}
</style>
<script type="text/javascript">
var picData = [
['http://www.nova.edu/hpd/otm/pics/4fun/11.jpg','show pic 1','http://www.thenightowl.com'],
['http://www.nova.edu/hpd/otm/pics/4fun/12.jpg','show pic 2','http://www.thenightowl.com'],
['http://www.nova.edu/hpd/otm/pics/4fun/13.jpg','show pic 3','http://www.thenightowl.com'],
['http://www.nova.edu/hpd/otm/pics/4fun/14.jpg','show pic 4','http://www.thenightowl.com']
];
//preload the pics
var picsObj = [];
for(i=0; i < picData.length; i++){
picsObj[i] = [];
picsObj[i][0] = new Image();
picsObj[i][0].src = picData[i][0];
picsObj[i].lnkText = picData[i][1];
}
function showPic(num){
img1Obj.style.display='inline';
img1Obj.src = picsObj[num][0].src;
img1Obj.href = picsObj[num][2].href;
}
window.onload=function(){
img1Obj = document.getElementById('img1');
//create the links
var ulObj = document.getElementById('linksCont');
for(i=0; i < picsObj.length; i++){
var newLi = document.createElement('li');
var newA = document.createElement('a');
newA.num = i;
newA.appendChild(document.createTextNode(picsObj[i].lnkText));
newA.href='';
newA.onclick=function(){showPic(this.num);return false;}
newLi.appendChild(newA);
ulObj.appendChild(newLi);
}
}
</script>
</head>
<body>
<div>
<a href=""><img src="" id="img1" alt="" /></a>
</div>
<ul id="linksCont"></ul>
</body>
</html>
Am I close?
Bookmarks