简体   繁体   English

如何通过图像 ID 将图像从一个 div 复制到另一个 div

[英]How do I copy an image from one div to another div by the image ID

I have 3 tabs.我有 3 个标签。 On the first tab is a link that opens the second tab.第一个选项卡上是打开第二个选项卡的链接。 The second tab has a series of images.第二个选项卡有一系列图像。 What I am trying to achieve is when a user clicks on any of the images in the second tab, that image is copied back to the div where the user first clicked to open this tab.我想要实现的是,当用户单击第二个选项卡中的任何图像时,该图像被复制回用户第一次单击以打开此选项卡的 div。 Every thing I have tried so far has failed.到目前为止,我尝试过的每件事都失败了。 I need to know the id of the clicked image and copy this image.我需要知道点击图像的 id 并复制此图像。 If the user then clicks another image it would copy that one as well.如果用户随后单击另一张图像,它也会复制该图像。

<div id="demoTabs">
    <ul>
        <li><a href="#bike">Bike</a></li>
        <li><a href="#coach">Coach</a></li>
        <li><a href="#truck">Truck</a></li>
    </ul>

<div id="coach">
    <div id="firstDiv">
        <img src="assets/img/english/calltoadventure.png" id="imgone" class="theImage" value="Copy"/> 
        <br>
        <img src="assets/img/english/cinemastudies.png" id="imgtwo" class="theImage" value="Copy"/>
    </div>
    <div id="myDiv">
        <img src="assets/img/english/calltoadventure.png" id="img19" class="theImage" value="Copy"/> 
        <br>
        <img src="assets/img/english/cinemastudies.png" id="img20" class="theImage" value="Copy"/>
    </div>
</div>

<div id="bike">
    <div id="secondDiv">
        <a href="#coach" class="open-tab" data-tab-index="1">
        <img src="assets/img/bike.png" id="english"/></a></div>
    </div>  

    <div id="truck"><img src="assets/img/truck.jpg"/></div>

</div>

</body>
<script>
$(document).ready(function() {
    var $selectedOption =""; //the option user clicks on which determins which tab to open
    var $theDiv = "";        // the div where the image to copy resides
    var $thecallingDiv = ""; // the div where the image is to be placed
    var $selectedImage = ""; // the image the user selects

    $('#demoTabs').tabs({ active: 0 });

    $('.open-tab').click(function (event) {
        $selectedOption = $(this).children('img').attr('id');
        $thecallingDiv = $(this).closest('div').attr('id');

        $('#demoTabs').tabs("option", "active", $(this).data("tab-index"));
    });

    $("img.theImage").on("click",function() {  
        // get the id of the image selected
        $subjectImage = $(this).attr('id');

        // get the id of the closest div
        $theDiv = $(this).closest('div').attr('id');


        $($selectedImage).clone().attr('id',$selectedImage).append($thecallingDiv);

    }); 

</script> 

I'm assuming you're using jQueryUI's tabs.我假设您正在使用 jQueryUI 的选项卡。

If so, fix your html structure because it does not comply with that libraries proposed structure.如果是这样,请修复您的 html 结构,因为它不符合该库建议的结构。

All tou have to do is clone the clicked image into the desired div你所要做的就是将点击的图像克隆到所需的 div 中

 $(document).ready(function() { const secondDiv = $('#secondDiv') $('#demoTabs').tabs({ active: 0 }); $('.open-tab').click(function (event) { $('a[href="#coach"]').click() }); $('#coach img').click(event => { secondDiv.append($(event.target).clone()) console.log(`${$(event.target).attr('id')} copied to first tab`) }); });
 img{cursor: pointer}
 <link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css'/> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=" crossorigin="anonymous"></script> <div id="demoTabs"> <ul> <li><a href="#bike">Bike</a></li> <li><a href="#coach">Coach</a></li> <li><a href="#truck">Truck</a></li> </ul> <div id="bike"> <div id="secondDiv"> <a href="#" class="open-tab" data-tab-index="1">Open 2nd tab</a> <br> </div> </div> <div id="coach"> <div id="firstDiv"> <img src="https://via.placeholder.com/50?text=1" id="imgone" class="theImage" value="Copy"/> <br> <img src="https://via.placeholder.com/50?text=2" id="imgtwo" class="theImage" value="Copy"/> </div> <div id="myDiv"> <img src="https://via.placeholder.com/50?text=3" id="imgthree" class="theImage" value="Copy"/> </div> </div> <div id="truck"></div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM