【发布时间】:2014-08-16 04:32:01
【问题描述】:
问:如何从包含一些图像的源 div(id="p2") 中获取目标 div(id="p1") 内的点击图像。
当前结果:只显示最后一张图片。如果我错了,请提出其他逻辑。 P.S 只需要 javascript。
在 {document.getElementById("p1").innerHTML="";} 之前需要一些逻辑,即脚本代码的最后一行。所以按照当前的逻辑,显示的是准确点击的图片而不是最后一张图片
下面是sn-p的代码:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<style>
.main{
height:400px;
width:200px;
margin:0px auto}
.p1{
height:200px;
width:200px;
background-size: 100% auto;
border-style:outset;
border-radius:2px;
}
.images{
width:50px;
height:50px;
border-radius:5px;
}
</style>
<body >
<div class="main" >
<div id="p1" class="p1"><img src="" alt="" /></div>
<div id="p2">
<input type="image" class="images" id="obj0" name="obj" onclick="myFunction()" src="http://www.picgifs.com/clip-art/flowers-and-plants/flowers/clip-art-flowers-511058.jpg"/></br>
<input type="image" class="images" id="obj1" name="obj" onclick="myFunction()" src="http://www.picgifs.com/clip-art/flowers-and-plants/flowers/clip-art-flowers-380016.jpg"/></br>
<input type="image" class="images" id="obj2" name="obj" onclick="myFunction()" src="http://www.picgifs.com/clip-art/flowers-and-plants/flowers/clip-art-flowers-293063.jpg"/></br>
<input type="image" class="images" id="obj3" name="obj" onclick="myFunction()" src="http://www.picgifs.com/clip-art/flowers-and-plants/flowers/clip-art-flowers-611834.jpg"/></br>
</div>
<script>
function myFunction(){
var x=document.getElementsByName("obj");
y=x.length;
for(var i=0;i<y;i++){
hi = document.getElementById('obj'+i).src;
//alert(hi); uncommenting I can get url of each picture
document.getElementById("p1").innerHTML="<img src='"+hi+"' height=100px ;width=100px centre />";
}
}
</script>
</div>
</body>
提前致谢!
【问题讨论】:
-
为什么不将输入图像的唯一id作为参数传递给myFunction()?例如myFunction("obj0")、myFunction("obj1") 等
标签: javascript html