javascript - 使用 JS/Jquery 在现有 HTML 元素中添加图像

标签 javascript jquery html

在我的网页上有Gridster小部件。这些小部件中包含可以删除的图像。现在我有一个 + 按钮,当用户单击它时,会打开一个显示图像列表的模式。现在我希望用户选择一个图像(单击在它上面)然后按 Add Image 然后图像应该被添加到指定的小部件中。

此外,以模式显示的图像是从服务器检索的,因此我无法手动放置 id 之类的元素来区分它们。我认为 jquery 中的 this 将有助于获取单击的特定图像。此外,添加的图像应具有与现有图像相同的结构。

'<div class="imagewrap"><img src= image i click > <input type="button" class="removediv" value="X" /></div></div>';

我还想用我添加的图像的 src 更新 textarea 字段,就像其他现有图像一样。

HTML:

<div class="gridster">
    <ul>

    </ul>

</div>
<button class="js-seralize btn btn-success mr-2">Serialize</button>
<textarea id="log"></textarea>

<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLongTitle">Add Icons</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">

            <img src="https://cdnd.icons8.com/wp-content/uploads/2015/07/Run-Command-100.png">
      <img src="https://png.icons8.com/metro/2x/chapel.png">
      <img src="https://png.icons8.com/metro/2x/boy.png">
      <img src="https://png.icons8.com/metro/2x/wacom-tablet.png">






      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Add Image</button>
      </div>
    </div>
  </div>
</div>

JS:

var gridster;


gridster = $(".gridster ul").gridster({
  widget_base_dimensions: [100, 100],
  widget_margins: [5, 5],
  helper: 'clone',
   serialize_params: function($w, wgd) {return {images: $w.find('textarea').val().trim() , col: wgd.col, row: wgd.row, size_x: wgd.size_x, size_y: wgd.size_y}},
  resize: {
    enabled: true
  }
}).data('gridster');


 var json = [{
    "html": "https://d30y9cdsu7xlg0.cloudfront.net/png/802768-200.png,https://d30y9cdsu7xlg0.cloudfront.net/png/802768-200.png,https://d30y9cdsu7xlg0.cloudfront.net/png/802768-200.png", //3 Images
    "col": 1,
    "row": 1,
    "size_y": 2,
    "size_x": 2
}, {
    "html":"https://d30y9cdsu7xlg0.cloudfront.net/png/802768-200.png,https://d30y9cdsu7xlg0.cloudfront.net/png/802768-200.png", // 2 Images
    "col": 4,
    "row": 1,
    "size_y": 2,
    "size_x": 2
},

{
    "html": "https://d30y9cdsu7xlg0.cloudfront.net/png/802768-200.png", // 1 Image
    "col": 6,
    "row": 1,
    "size_y": 2,
    "size_x": 2
},


{
    "html": "https://image.flaticon.com/icons/svg/67/67994.svg,https://d30y9cdsu7xlg0.cloudfront.net/png/802768-200.png",  // 2 Images
    "col": 1,
    "row": 3,
    "size_y": 1,
    "size_x": 1
}, {
    "html": "https://image.flaticon.com/icons/svg/67/67994.svg", //1 Image
    "col": 4,
    "row": 3,
    "size_y": 1,
    "size_x": 1
},

{
    "html": "https://image.flaticon.com/icons/svg/67/67994.svg,https://d30y9cdsu7xlg0.cloudfront.net/png/802768-200.png", //2 Images
    "col": 6,
    "row": 3,
    "size_y": 1,
    "size_x": 1
}

];








for(var index=0;index<json.length;index++) {
    var images = json[index].html.split(',');
        var imageOutput = "";

        for(var j = 0; j < images.length; j++) {
        imageOutput += '<div class="imagewrap"><img src='+ images[j] +'> <input type="button" class="removediv" value="X" /></div></div>';
        }

gridster.add_widget('<li class="new" ><button class="addmorebrands" style="float: left;">+</button><button class="delete-widget-button" style="float: right;">-</button>' + imageOutput + '<textarea>'+json[index].html+'</textarea></li>',json[index].size_x,json[index].size_y,json[index].col,json[index].row);
}









$('.removediv').on('click', function () {
  $(this).closest('div.imagewrap').remove();
});


 $(document).on("click", ".delete-widget-button", function() {
        var gridster = $(".gridster ul").gridster().data('gridster');
        gridster.remove_widget($(this).parent());
    });


$('.js-seralize').on('click', function () {
    var s = gridster.serialize();
    $('.gridster ul li').each((idx, el)=>{ // grab the grid elements
       s[idx].html = $('textarea', el).html(); // add the html key/values

    json_variable=JSON.stringify(s)
   });



    $('#log').val(json_variable);
});

$(document).on("click", ".addmorebrands", function() {
        $('#exampleModalCenter').modal('show');

    });

有人请帮助我,因为我发现它真的很难得到它

Fiddle Link

最佳答案

添加图像部分现在已启动并运行,至少在其原始代码中,修改并添加了本节中的一些代码:

//EDITS
var parentLI;
$(document).on("click", ".addmorebrands", function() {
    parentLI = $(this).closest('li');
    $('#exampleModalCenter').modal('show');
    $('#exampleModalCenter img').click(function(){
        $(this).addClass('preselect');
        $(this).siblings().removeClass('preselect');
        selectedImageSRC = $(this).attr('src');
    });
});

$('#add-image').click(function(){
    parentLI.append('<div class="imagewrap"><img src="'+selectedImageSRC+'"> 
    <input type="button" class="removediv" value="X" /></div>');
    parentLI.children('textarea')
       .append(', '+selectedImageSRC);
    $('#exampleModalCenter').modal('hide');
});

还有这个:

.preselect{
  border: 2px solid white;
  background: green
}

.preselect:hover{
  border: 1px solid white;
  background: lightgreen
}

我将留给您调整每个单元格内图像大小的部分,以及其他所有内容:) https://jsfiddle.net/0ndht0zh/23/

更新

此代码仅适用于运行时存在的.removediv:

$('.removediv').on('click', function () {
    $(this).closest('div.imagewrap').remove();
});

这就是为什么先前存在的图像会被删除,但您稍后添加的图像不会被删除的原因。我不是专家,所以请研究更多技术解释,但基本上:

下面的代码几乎是一样的,除了因为它附加到 document(但applied .removediv),它实际上存在于这段代码运行时,事件将始终找到到达 .removediv 的路径。

$(document).on('click', '.removediv', function () {
    $(this).closest('div.imagewrap').remove();
});

更新的 fiddle :https://jsfiddle.net/0ndht0zh/27/

还记得在删除时更新textarea内容

关于javascript - 使用 JS/Jquery 在现有 HTML 元素中添加图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49503008/

相关文章:

javascript - 为什么在将作用域变量传递给组件绑定(bind)时,Angular 1.5 双向绑定(bind)会失败?

javascript - 使用 jQuery 将数(天)转换为日、月和年

php - 如何使用 jQuery forms.js 将 LI 附加到 UL

javascript - 计算两个日期之间的百分比

javascript - 如何判断浏览器是否为HTML5 Canvas提供硬件加速

jquery - 动态div不自动滚动

html - 具有向下钻取功能的 Angular 5 表

javascript - 当来自 commonJS 模块的 .call(this) 时,如何创建此窗口

javascript - 如何处理 react/redux 中的副作用?

javascript - 如何使用 jQuery 公开 IFrame 的 DOM?