猿问

setAttribute问题,为什么点击链接不能在占位符的位置显示图片,而是转到另一个窗口???

<title>无标题文档</title>
<script type="text/javascript">
function shopPic(whichpic){
var placeholder=document.getElementById("placeholder");
placeholder.setAttribute("src",whichpic.getAttribute("href"));
}
</script>
</head>


<body>
<h1>Snapshot</h1>
<ul>
<li><a href="image/1.jpg" title="1">1</a></li>
<li><a href="image/2.jpg" title="2">2</a></li>
<li><a href="image/3.jpg" title="3">3</a></li>
<li><a href="image/4.jpg" title="4">4</a></li>
</ul>
<img id="placeholder" src="image/3.jpg" alt="占位符" />
</ul>
</body>
</html>


lzy瑜
浏览 1489回答 2
2回答

Ewall_

你事件onclick都没有,哪里来的图片转换<body> <h1>Snapshot</h1> <ul id="picgroup"> <li><a href="image/1.jpg" title="1">1</a></li> <li><a href="image/2.jpg" title="2">2</a></li> <li><a href="image/3.jpg" title="3">3</a></li> <li><a href="image/4.jpg" title="4">4</a></li> </ul> <img id="placeholder" src="image/3.jpg" alt="占位符" /> <script type="text/javascript"> function picgroup(){ var picgroup=document.getElementById("picgroup"); var aLinks=picgroup.getElementsByTagName("a"); for(i=0;i<aLinks.length;i++){ aLinks.onclick=function(){ showPic(this); return flase; } } } function showPic(whichpic){ var placeholder=document.getElementById("placeholder"); placeholder.setAttribute("src",whichpic.getAttribute("href")); } </script> </body>

_潇潇暮雨

需要阻止默认事件,下面是代码:<html> <title>无标题文档</title> <script type="text/javascript"> function shopPic(whichpic){  event.preventDefault() var placeholder=document.getElementById("placeholder"); console.log(whichpic.getAttribute("href")) placeholder.setAttribute("src",whichpic.getAttribute("href")); } </script> </head> <body> <h1>Snapshot</h1> <ul> <li><a href="image/1.jpg" title="1" onclick="shopPic(this)">1</a></li> <li><a href="image/2.jpg" title="2" onclick="shopPic(this)">2</a></li> <li><a href="image/3.jpg" title="3" onclick="shopPic(this)">3</a></li> <li><a href="image/4.jpg" title="4" onclick="shopPic(this)">4</a></li> </ul> <img id="placeholder" src="image/3.jpg" alt="占位符" /> </ul> </body> </html>
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答