Javascript占位符

时间:2011-08-02 22:24:22

标签: javascript

  

可能重复:
  Javascript placeholders

嗯..我真的很喜欢javascript而且很挣扎。我的代码如下。使用更多代码更容易解​​释。所以基本上想要在每个占位符中显示每个图像,但是按照我点击它们的顺序。所以,如果我点击img 3,这将转到占位符1然后下一个我点击将转到占位符2,如果这是有道理的。 (这个棘手的位是一旦选择了一个选项我想再次不允许该选项。要么有错误消息说已经使用过或从列表中消失)

    <script type="text/javascript" language="javascript">
function showPic (whichpic) { 
    if (document.getElementById) { 
        document.getElementById('placeholder1').src = whichpic.href;
        document.getElementById('placeholder2').src = whichpic.href;
        document.getElementById('placeholder3').src = whichpic.href;
        if (whichpic.title) { 
            document.getElementById('desc').childNodes[0].nodeValue = whichpic.title; 
        } else { 
            document.getElementById('desc').childNodes[0].nodeValue = whichpic.childNodes[0].nodeValue; 
        } 
        return false; 
    } else { 
        return true; 
    } 
}
</script>

<div style="border: 1px solid #999; padding: 1em; margin: 0 0 15px 0;">
<ul>
<li><a onclick="return showPic(this)" href="images/img1.jpg" title="img 1">img 1</a></li>
<li><a onclick="return showPic(this)" href="images/img2.jpg" title="img 2">img 2</a></li>
<li><a onclick="return showPic(this)" href="images/img3.jpg" title="img 3">img 3</a></li>
</ul>

<p id="desc">Choose an image to begin</p>
<img id="placeholder1" src="images/blank.gif" alt="" />
<img id="placeholder2" src="images/blank.gif" alt="" />
<img id="placeholder3" src="images/blank.gif" alt="" />
</div>

1 个答案:

答案 0 :(得分:0)

您可以像这样删除<li>。后:

if (whichpic.title) { 
            document.getElementById('desc').childNodes[0].nodeValue = whichpic.title; 
} else { 
            document.getElementById('desc').childNodes[0].nodeValue = whichpic.childNodes[0].nodeValue; 
} 

之前:

return false;

添加:

var node = whichpic;
while(node.nodeName != 'LI' && (node = node.parentNode));
node.parentNode.removeChild(node);