大神帮忙看下这段js代码

为何每次点击之后不会在下面替换掉图片,而是跳出来一个新的页面,貌似prepareGallery()函数没有被调用

html代码

 <!DOCTYPE html>
<html lang = "en">
<head>
    <meta charset = "UTF-8">
    <title>let's do it</title>
    <link rel = "stylesheet" href = "D1css.css" media = "screen" />
</head>
<body>

<h1>picture</h1>
<ul id = "imagegallery">
    <li>
        <a href = "img/tlswft.jpg" title = "my lady">泰勒斯威夫特</a>
    </li>
    <li>
        <a href = "img/arpxn.jpg" title = "god father">阿尔帕西诺</a>
    </li>
    <li>
        <a href = "img/wolf1.jpg" title = "wolf">狼</a>
    </li>
    <li>
        <a href = "img/xslj.jpg" title = "clown" >希斯莱杰</a>
    </li>
</ul>
<img id = "placeholder" src = "img/bai.jpg" alt = "my image gallery"/>
<p id = "description">choose your img</p>
<script  src = DOM1.js></script>
</body>
</html>

js代码

 /**
 * Created by kaka on 2017/3/30.
 */

function prepareGallery() {
    if (!document.getElementsByTagName()) return false;
    if (!document.getElementById()) return false;
    if (!document.getElementById("imagegallery")) return false;
    var gallery = document.getElementById("imagegallery");

    var links = gallery.getElementsByTagName("a");
    for (var i = 0; i < links.length; i++) {
        links[i].onclick = function () {
           return showPic(this);
        }
    }

}
function sss() {

    alert(dwadwadwad);
}
function showPic(whichpic) {
    if (!document.getElementById("placeholder")) return false;
    var source = whichpic.getAttribute("href");
    var placeholder = document.getElementById("placeholder");
    if (placeholder.nodeName != "IMG") return false;
    placeholder.setAttribute("src", source);
    if (document.getElementById("description")) {
        var text = whichpic.getAttribute("title") ? whichpic.getAttribute("title") : "";
        var description = document.getElementById("description");
        if (description.firstChild.nodeType == 3) {
            description.firstChild.nodeValue = text;
        }
    }
    return true;
}


查看全部
github_38243408
Ertist
2017/04/15 06:27
  • javascript
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

2个回复