2 qq 34709723 qq_34709723 于 2016.04.19 16:08 提问

做了一个占位符 但是每次点开相关的链接 不能再占位符上显示 而是直接新打开了一个窗口

html:

 <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>Snapshots</h1>
<ul id="love">
    <li>
        <a href="美.jpg" title="A good girl">美</a>
    </li>
    <li>
        <a href="女.jpg" title="A good girl">女</a>
    </li>
    <li>
        <a href="丽.jpg" title="A good girl">丽</a>
    </li>
    <li>
        <a href="莉.jpg" title="A good girl">莉</a>
    </li>
</ul>
<img id="placeholder" src="风景.jpg" alt="my image gallery" width="100" height="100"/>
<p id="description" >Choose an image</p>
<script src="lianxi.js"></script>
</body>
</html>


JS:
window.onload=prepareGallery;
function prepareGallery(){
    if(!document.getElementsByTagName) return false;
    if(!document.getElementById) return false;
    if(!document.getElementById("love")) return false;
    var gallery = document.getElementById("love");
    var links = gallery.getElementsByTagName("a");
    for ( var i=0;i<links.length;i++) {
        links[i].onclick = function () {
            return showPic(this) ? false:true;
        }
    }
}
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 false;

}

1个回答

showbo
showbo   Ds   Rxr 2016.04.19 16:55
 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;/////////////////////////

}
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!