图片点击切换效果怎么不行啊,求帮忙看看

本来在 showPic函数里不修改title属性可以正常运行,但是一加上description的文字可以运行,但是一加上后面那一段就不行了,求帮忙

<!doctype html>

<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <ul id="imagegellery">
        <li>
            <a href="1.jpg" title="第1张图片">第1张图片</a>
        </li>
         <li>
            <a href="2.jpg" title="第2张图片">第2张图片</a>
        </li>
         <li>
            <a href="3.jpg" title="第3张图片">第3张图片</a>
        </li>
    </ul>
<script>
    function addLoadEvent(func){
        var oldonload=window.onload;
        if(typeof window.onload!='function'){
            window.onload=func;
        }else{
            window.onload=function(){
                oldonload();
                func();
            }
        }
    }
    function insertAfter(newElement,targetElement){
        var parent=targetElement.parentNode;
        if(parent.lastChild==targetElement){
            parent.appendChild("newElement");
        }else{
            parent.insertBefore(newElement,targetElement.nextSibling);
        }
    }
    function preparePlaceholder(){
        var placeholder=document.createElement("img");
        placeholder.setAttribute("id","placeholder");
        placeholder.setAttribute("src","4.jpg");
        placeholder.setAttribute("alt","默认图片");
        var description=document.createElement("p");
         var description=document.createElement("id","description");
         var desctext=document.createTextNode("默认图片");
         description.appendChild(desctext);
         var gallery=document.getElementById("imagegellery");
         insertAfter(placeholder,gallery);
         insertAfter(description,gallery);
    }
    function preareGallery(){
    var gallery=document.getElementById("imagegellery");
    var links=gallery.getElementsByTagName("a")
    for (var i = 0; i < links.length; i++) {
        links[i].onclick=function(){
            return showPic(this);
        }
        links[i].onkeypress=links[i].onclick;
    };
}
function showPic(whichpic){
    var source=whichpic.getAttribute("href");
    var placeholder=document.getElementById("placeholder")
     placeholder.setAttribute("src",source)
     if(whichpic.getAttribute("title")){
     var text=whichpic.getAttribute("title");}
     else{var text="";}
     var description=document.getElementById("description")
     if(description.firstChild.nodeType==3){
     description.firstChild.nodeValue=text;
 }     
    return false;
}
addLoadEvent(preparePlaceholder)
addLoadEvent(preareGallery)

</script>
    
</body>
</html>

慕粉4084340
浏览 1326回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript