开发者

How to change content of a div

I know this question has been asked before, but if someone could explain in greater detail it would be awesome. I have a div and a table and an image inside this div. I am making a image gallery, so I have two links forward and back, how do I make these links change the image, or rather all the content inside the div. I am very new to javascript, actually I know nothing at all about it, if I could开发者_JS百科 get a step by step instruction that would be awesome, I have tried so of the other post codes but can not get it to work, so I have no idea what I am doing wrong.


Sample HTML:

<html>
<head><title>Dummy page</title></head>
<body>
  <div id="divID">
    <img id="backImg" src="http://test.com/sample.jpg">
  </div>
</body>
</html>

To change the whole div:

var div = document.getElementByID('divID');
div.innerHTML = "<b>This is some html</b>";

To just change the image:

var img = document.getElementByID('backImg');
img.src = "http://www.host.com/image.jpg';


Here's a good example that I made that might help. Try it out -- http://jsfiddle.net/SuperBoi45/XMSGe/

Here's the code for a quick look:

HTML:

<button id="before">Previous</button>
<button id="next">Next</button>
<div id="imageGallery"></div>

JavaScript:

function $(id) {
    return document.getElementById(id);
}
var foward = null,
    back = null,
    images = [
        "http://www.toxel.com/wp-content/uploads/2009/04/conceptcar04.jpg",
        "http://politicolnews.com/wp-content/uploads/2010/01/bill-gates-car.jpg",
        "http://www.youthedesigner.com/wp-content/uploads/2008/05/car-wallpapers19.jpg",
        "http://d2o7bfz2il9cb7.cloudfront.net/main-qimg-555c9ae9cfd364db5307b2e8d717a00d",
        "http://d2o7bfz2il9cb7.cloudfront.net/main-qimg-01589fb639efec5433a3e30a8a8dd449",
        "http://d2o7bfz2il9cb7.cloudfront.net/main-qimg-31f8cbd6627edc1ba9ef2828f3423fdf",
        "http://d2o7bfz2il9cb7.cloudfront.net/main-qimg-0f47a0c2db85b5d7c134d41bcdc65b2e",
        "http://d2o7bfz2il9cb7.cloudfront.net/main-qimg-739fd589778207e542a6e31873a24433",
        "http://d2o7bfz2il9cb7.cloudfront.net/main-qimg-6056a5df58462ea4951a2b328243b7a2",
        "http://d2o7bfz2il9cb7.cloudfront.net/main-qimg-59028363fc0f7d0ee7aa1ebc5e72713a",
        "http://d2o7bfz2il9cb7.cloudfront.net/main-qimg-eb09864be7a1fbe7e821bc1ee08c3a8b",
        "http://d2o7bfz2il9cb7.cloudfront.net/main-qimg-50bd88090e05a452bba67d510ba4a0cc",
        "http://d2o7bfz2il9cb7.cloudfront.net/main-qimg-191b37eea296e90a242d24db90824c5c"
        ];

var img = new Image();
for (var i = 0; i < images.length; i++) { // caching the images for performance boost
    img.src = images[i];
}

var image = {
    count: -1,
    next: function() {
        if (image.count == (images.length) - 1) return false;
        image.count += 1;
        $('imageGallery').innerHTML = "<img src=\"" + images[image.count] + "\"\/>";
    },
    previous: function() {
        if (image.count <= 0) return false;
        image.count -= 1;
        $('imageGallery').innerHTML = "<img src=\"" + images[image.count] + "\"\/>";
    }
};

foward = image.next;
back = image.previous;
$('next').addEventListener("click", foward);
$('before').addEventListener("click", back);

The best thing about this is that all you have to do is add a new image URL to the array and it will still work. If you want to change the images but not all the content in the div, I'd recommend putting in another div outside the image gallery.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜