簡體   English   中英

如何從源div中獲取包含目標圖像的目標div中的單擊圖像

[英]How to get clicked image inside target div from source div which contain some images

問:如何從包含一些圖像的源div(id =“ p2”)獲取目標div(id =“ p1”)中的點擊圖像。

當前結果:僅顯示最后一張圖像。如果我錯了,請提出其他邏輯。 僅需PS javascript。

在{document.getElementById(“ p1”)。innerHTML =“”;}(即腳本代碼的最后一行)之前需要一些邏輯。 因此,根據當前邏輯,將顯示確切單擊的圖像而不是最后一個圖像

下面是代碼片段:

<!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。 然后,您需要獲取已傳遞的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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM