Javascript 单击缩略图时使图像显示在单独的部分中

Javascript 单击缩略图时使图像显示在单独的部分中,javascript,jquery,html,Javascript,Jquery,Html,我期待有一个图像出现在一个单独的部分后,图像的缩略图是在上述部分点击 索引代码: <section id="select-image"> <h2>Step 1. Select an image</h2> <p>Select your prefered image</p> <div id="ug-images"><img src="/images/ugima

我期待有一个图像出现在一个单独的部分后,图像的缩略图是在上述部分点击

索引代码:

        <section id="select-image">
        <h2>Step 1. Select an image</h2>
        <p>Select your prefered image</p>
        <div id="ug-images"><img src="/images/ugimage1.jpg"></div>
    </section>
    <section id="add-text">
        <h2>Step 2. Add Text</h2>
        <input id="text" type="text" value="Customise me!">
    </section>
    <section id="style-image">
        <h2>Step 3. Style it!</h2>
        <div id="workspace">

第一步。选择一个图像
选择您喜欢的图像

第二步。添加文本 第三步。风格!
javascript代码:

    $(document).on('click', '#ug-images', function() {
  var url = $(this).data('url');
  $("#workspace img").remove();
  var img = $("<img>").attr('src', url);
  $("#workspace").append(img);
});
$(文档).on('click','ug images',function(){
var url=$(this.data('url');
$(“#工作区img”).remove();
变量img=$(“

为了清楚起见,我希望在id=Select image中选择的缩略图显示在id=workspace中

您只需创建图像的副本并将其附加到workspace即可

var $workspace = $('#workspace'), //simply caching the selector
    $img = $();

$('#ug-images').on('click', 'img', function () {
    $img.remove();
    $img = $(this).clone().appendTo($workspace);
});
$(document).on('click', '#ug-images', function () {
  var $newImg = $(this).clone();
  $('#workspace').append($newImg);
});
这里有一个演示:

像这样的东西


您不需要处理URL属性。您可以克隆单击的图像并将其附加到“#工作区”

在这里查看:


jQuery(文档).ready(函数(){
jQuery(“#ug图像”)。单击(函数(){
$(“#工作区img”).remove();
var url=$(this.find('img').attr('src');
警报(url);
$('#workspace').html('')
});
});
试试这个

如果只想显示选定的图像

或 如果要将所单击的每个图像附加到


这个很好,谢谢!
$(document).on('click', '#ug-images', function () {
  var $newImg = $(this).clone();
  $('#workspace').append($newImg);
});
<script>
jQuery(document).ready(function(){
    jQuery("#ug-images").click(function(){
        $("#workspace img").remove();
        var url = $(this).find('img').attr('src');
        alert(url);
        $('#workspace').html('<img  src="'+url+'" />')
    });
});
</script>
 $(document).on('click', '#ug-images', function() {
 var img = $(this).find('img').clone();
 $("#workspace").html(img);
});
$(document).on('click', '#ug-images', function() {
 var img = $(this).find('img').clone();
 $("#workspace").append(img);
});