如何从包含一些图像的源div中获取目标div内的单击图像

时间:2014-08-16 04:32:02

标签: javascript html

问:如何从源div(id =" p2")中获取包含一些图像的目标div(id =" p1")内的点击图像。

当前结果:仅显示最后一张图片。如果我错了,请提出其他逻辑。 P.S只需要javascript。

在{document.getElementById(" p1")。innerHTML ="&#34 ;;}之前需要一些逻辑,即脚本代码的最后一行。因此,显示精确点击的图像而不是最后一个图像,这是根据当前逻辑

以下是代码段:

<!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>

提前致谢!

1 个答案:

答案 0 :(得分:0)

你需要有一个参数来传递刚刚点击的东西的ID。然后,您需要获取已传递的ID的属性。

你走了:

<!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(this)" 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(this)" 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(this)" 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(this)" src="http://www.picgifs.com/clip-art/flowers-and-plants/flowers/clip-art-flowers-611834.jpg" /></br>
        </div>
        <script>

            function myFunction(id) {

                document.getElementById("p2").onclick = function () {
                document.getElementById("p1").innerHTML = "<img src='" + id.getAttribute("src") + "' height=100px ;width=100px centre />";

                }
            }
        </script>
    </div>
</body>