开发者

No Ouput to DOM Tree

In the following code, my attempts at creating the preparePlaceholder on the the fly has failed, and the only error is "parent is not defined" from the error console.

I'm modifying this example for use with a JS object literal and could use the sage wisdom of StackOverflow.

TIA

imageGallery={

    // IDs
    placeHolderID:'placeholder',
    imageNavListID:'imagegallerylist',

    // CSS Classes

    init:function(){

        if(!document.getElementById || !document.createTextNode){return;}
        imageGallery.preparePlaceholder();// Call to preparePlacholder
        // Prepare Gallery:
        imageGallery.navList = document.getElementById(imageGallery.imageNavListID);
        if(!imageGallery.navList){return;}
        var links = imageGallery.navList.getElementsByTagName('a');
        for(var i = 0; i<links.length;i++){
        links[i].onclick = function(){
        // Call to showPic function:    
        return imageGallery.showPic(this) ? false : true;
        }

      }


},


    showPic:function(whichpic){
        imageGallery.pHolder=document.getElementById(imageGallery.placeHolderID);
        if(!imageGallery.pHolder || imageGallery.pHolder.nodeName != "IMG"){return;}
        var source = whichpic.getAttribute("href");
        imageGallery.pHolder.setAttribute("src",source);
        if(document.getElementById("description")){
            // var text = whichpic.getAttribute("title");
            var text = whichpic.getAttribute("title") ? whichpic.getAttribute("title") : "";
            var description = document.getElementById("description");
            if(description.firstChild.nodeType == 3){
            description.firstChild.nodeValue = text;

            }

        }
        return true;

},

    preparePlaceholder:function(){
        var placeholder = document.createElement("img");
        placeholder.setAttribute("id", "placeholder");
        placeholder.setAttribute("src","images/placeholder.gif");
        placeholder.setAttribute("alt","My Image Gallery");
        // alert(placeholder);
        var description = document.createElement("p");
        description.setAttribute("id","description");
        var desctext = document.createTextNode("Choose an Image");
        descripti开发者_运维技巧on.appendChild(desctext);
        var gallery = document.getElementById("imageGallery");
        imageGallery.insertAfter(placeholder,imageGallery);
        imageGallery.insertAfter(description,imageGallery.placeholder);

        // alert("Function Called");
},

// Utility Functions
      insertAfter:function(newElement,targetElement){
        var parent = targetElement.parentNode;
        if(parent.lastChild == targetElement){
            parent.appendChild(newElement);

        } else {

            parent.insertBefore(newElement,targetElement.nextSibling);

        }

    },
    addLoadEvent:function(func){
        var oldonload = window.onload;
        if(typeof window.onload != 'function'){
            window.onload = func;
        }else{
            window.onload = function(){
                oldonload();
                func();
            }

        }

    }

}   
// End Utility Functions    

imageGallery.addLoadEvent(imageGallery.init);   

// window.onload=imageGallery.init;


  <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="uft-8" />
    <title>Image Gallery</title>
    <link rel="stylesheet" href="styles/layout.css" type="text/css" media="screen" title="layout" charset="utf-8" />

</head>
<body>
    <h1>Snapshots</h1>
    <ul id="imagegallerylist">
    <!--Links Here-->
    </ul>   
<script src="scripts/imageGallery.js"></script>
</body> 
</html> 


Have an example here which doesn't produce errors. I have removed your comments and put in comments where I have changed the code.

JavaScript:

var gallery = document.getElementById("imageGallery");
// Changed from imageGallery to gallery
imageGallery.insertAfter(placeholder, gallery);
// Changed from imageGallery.placeholder to placeholder
imageGallery.insertAfter(description, placeholder);

HTML:

<div id="imageGallery"></div> <!-- Added this div -->
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜